Golang automatic code formatting : Code like a Pro

Why Format your code?

Everyone loves clean readable and beautifully organized code using tabs/spaces (whatever you like), short lines etc. As a developer, while writing code, you should not spend time counting the tabs/spaces, instead let the tools handle your code formatting for you and that too automatically.

In this short post, we will learn how you can use golines to automagically format all your golang code

Implementing Golines: a Golang formatter

  • Installing golines
go install github.com/segmentio/golines@latest
  • Using golines from VSCode
  • Go into the VSCode settings menu, scroll down to the section for the Run on Save extension, click the Edit in settings.json link
  • Set the emeraldwalk.runonsave key as follows
  • "emeraldwalk.runonsave": { "commands": [ { "match": "\\.go$", "cmd": "golines ${file} -w" } ] }
  • Save the settings and restart VSCode
  • (optional) using golines from CLI
golines -w "path to *.go files"

Summary

golines together with vscode helps you auto-format your code

from

myMap := map[string]string{"first key": "first value", "second key": "second value", "third key": "third value", "fourth key": "fourth value", "fifth key": "fifth value"}

To

myMap := map[string]string{
"first key": "first value",
"second key": "second value",
"third key": "third value",
"fourth key": "fourth value",
"fifth key": "fifth value",
}

Isn’t this beautiful, I know it is ;)

--

--

--

Senior Principal Architect & Developer Evangelist @ Red Hat ♦ Loves K8s, OpenShift, Cloud-Native, Serverless, Hybrid-Multi-Cloud, Distributed Systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to implement an object repository in selenium with .properties file

What is AWS Timestream?

FizzBuzz: The Breakdown

Agile Principles

It's Local, It’s Parsed!

Your way to inherit class may be wrong 【Ruby on Rails】

IN THE WORLD OF SOFTWARE ENGINEERS: PLATFORMS, LANGUAGES AND JOB SATISFACTION

Nearest Plans | NFT Panda: World of Fantasy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Karan Singh

Karan Singh

Senior Principal Architect & Developer Evangelist @ Red Hat ♦ Loves K8s, OpenShift, Cloud-Native, Serverless, Hybrid-Multi-Cloud, Distributed Systems

More from Medium

Five Minutes to Start Learning Unit Test in Golang

Responsive Terminal Applications in Golang

Blokhus Watch Tower

Testing Web Server: Part 3

Building APIs in Go beyond Hello World