Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
A golang formatter that fixes long lines
License
golangci/golines
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Golines is a Go code formatter that shortens long lines,in addition to all the formatting fixes done bygofmt.
This repository is a fork ofsegmentio/golines.
The original repository will probably be archived in Q4 2025.
First, install the tool:
go install github.com/golangci/golines@latest
Then, run:
golines.# or to write the files in place:golines -w.
The paths can be either directories or individual files.If no paths are provided, then input is taken fromstdin (as withgofmt).
By default, the results are printed tostdout.To overwrite the existing files in place, use the-w flag.
See thisbefore andafter view of a file with very long lines.More example pairs can be found in thetestdata directory.
The tool has been tested on a variety of inputs, but it's not perfect.Among other examples, the handling of long lines in comments could be improved.If you see anything particularly egregious, please report it via an issue.
Some other options are described in the sections below.Rungolines --help to see all available flags and settings.
By default, the tool tries to shorten lines that are longer than 100 columnsand assumes that 1 tab = 4 columns.The latter can be changed via the-m and-t flags respectively.
Running the tool with the--dry-run flag will show pretty, git-style diffs.
Shortening long comment lines is harder than shortening codebecause comments can have arbitrary structure and format.golines includes some basic logic for shortening single-line (i.e.,//-prefixed) comments,but this is turned off by default since the quality isn't great.To enable this feature anyway, run with the--shorten-comments flag.
By default, the tool will usegoimportsas the base formatter (if found), otherwise it will revert togofmt.An explicit formatter can be set via the--base-formatter flag;the command provided here should accept its input viastdin and write its output tostdout.
By default, the tool will not format any files that look like they're generated.If you want to reformat these too, run with the flag--ignore-generated=false.
There are several possible ways to split lines that are part ofmethod chains.The original approach taken bygolines was to split on the args, e.g.:
myObj.Method(arg1,arg2,arg3,).AnotherMethod(arg1,arg2,).AThirdMethod(arg1,arg2,)
Starting in version 0.3.0, the tool now splits on the dots by default, e.g.:
myObj.Method(arg1,arg2,arg3).AnotherMethod(arg1,arg2).AThirdMethod(arg1,arg2)
The original behavior can be used by running the tool with the--no-chain-split-dots flag.
Important
This option is deprecated and will be potentially removed in a future version.Seethis issue andthis issue for more details.
In addition to shortening long lines, the tool also aligns struct tag keys;see the associatedbeforeandafterexamples in thetestdata directory.To turn this behavior off, run with--no-reformat-tags.
Add the following lines to your vimrc, substituting128 with your preferred line length:
letg:go_fmt_command="golines"letg:go_fmt_options= {\'golines':'-m 128',\}
- Install theGo extension.
- Edit the
settings.jsonfile by opening it in the editor with the Preferences:Open User Settings (JSON)orPreferences: Open Workspace Settings (JSON)command in the Command Palette (Ctrl+Shift+P). - Add the following lines:
"go.formatTool":"custom","go.alternateTools": {"customFormatter":"golines" }
- Optional: to define custom arguments for the formatter, add the following lines:
"go.formatTool":"custom","go.formatFlags": ["-m","128"],"go.alternateTools": {"customFormatter":"golines" }
- Save the settings and restart VSCode
- Go into the Goland settings and click "Tools" -> "File Watchers" then click the plus to create a new file watcher
- Set the following properties:
- Name:
golines - File type:
Go files - Scope:
Project Files - Program:
golines - Arguments:
$FilePath$ -w - Output paths to refresh:
$FilePath$
- Name:
- In the "Advanced Options" section, uncheck theAuto-save edited files to trigger the watcher setting
- Confirm by clicking OK
- Activate your newly created file watcher in the Goland settings under "Tools" -> "Actions on save"
Theminimum version ingo.modis the minimum required version of Go for any given version ofgolines.
The standard Go formatting tools (gofmt,goimports, etc.) are great, butdeliberately don't shorten long lines;instead, this is an activity left to developers.
While there are different tastes when it comes to line lengths in go, we've generally foundthat very long lines are more difficult to read than their shortened alternatives.As an example:
myMap:=map[string]string{"first key":"first value","second key":"second value","third key":"third value","fourth key":"fourth value","fifth key":"fifth value"}
vs.
myMap:=map[string]string{"first key":"first value","second key":"second value","third key":"third value","fourth key":"fourth value","fifth key":"fifth value",}
We builtgolines to give Go developers the option to automatically shorten long lines,like the one above, according to their preferences.
More background and technical details are available inthis blog post.
For each input source file,golines runs through the following process:
- Read the file, break it into lines
- Add a specially-formatted annotation (directive) to each line that's longerthan the configured maximum
- UseDave Brophy's excellentdecorated syntax tree library to parse the codeplus added annotations
- Do a depth-first traversal of the resulting tree, looking for nodesthat have an annotation on them
- If a node is part of a line that's too long, shorten it by alteringthe newlines around the node and/or its children
- Repeat steps 2-5 until no more shortening can be done
- Run the base formatter (e.g.,
gofmt) over the results, write these to eitherstdoutor the source file
Seethis blog post for more technical details.
About
A golang formatter that fixes long lines
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- Go99.5%
- Makefile0.5%