Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Originally published atMedium

     

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 usegolines 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 modeExit fullscreen mode
- Save the settings and restart VSCode
Enter fullscreen modeExit fullscreen mode
  • (optional) using golines from CLI```

golines -w "path to *.go files"

### Summarygolines together with vscode helps you autoformatt your code #### from
Enter fullscreen modeExit 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 modeExit 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 modeExit fullscreen mode

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
kakhavk profile image
Kakhaber Kashmadze
  • Joined

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

CollapseExpand
 
mareoraft profile image
MareoRaft
  • Joined

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

usage:
go fmt path/to/your/package

I read about it ongo.dev/blog/gofmt

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromkaran singh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp