DEV Community

Cover image for Golang automatic code formatting : Code like a Pro
karan singh
karan singh

Posted on β€’ Originally published at Medium

9 2

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"
            }
        ]
    }


Enter fullscreen mode Exit fullscreen mode
- Save the settings and restart VSCode
Enter fullscreen mode Exit fullscreen mode
  • (optional) using golines from CLI ```

golines -w "path to *.go files"

### Summary
golines together with vscode helps you autoformatt your code 
#### from 
Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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 ;)


![](https://raw.githubusercontent.com/ksingh7/blogs/main/posts/assets/thats-all-folks.gif)
Enter fullscreen mode Exit fullscreen mode

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (2)

Collapse
 
kakhavk profile image
Kakhaber Kashmadze β€’

It is better than gofmt because that it also organizes imports! Thank you

Collapse
 
mareoraft profile image
MareoRaft β€’

Why not use Go's builtin code formatter, gofmt?

usage:
go fmt path/to/your/package

I read about it on go.dev/blog/gofmt

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay