{"id":13723003,"url":"https://github.com/segmentio/golines","last_synced_at":"2025-08-07T10:09:33.346Z","repository":{"id":37985987,"uuid":"211984068","full_name":"segmentio/golines","owner":"segmentio","description":"A golang formatter that fixes long lines","archived":false,"fork":false,"pushed_at":"2025-04-14T19:02:32.000Z","size":110,"stargazers_count":1033,"open_issues_count":70,"forks_count":65,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-04-22T09:20:06.919Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/segmentio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-01T00:34:25.000Z","updated_at":"2025-04-21T20:36:03.000Z","dependencies_parsed_at":"2024-06-18T12:40:38.606Z","dependency_job_id":"e8cb8168-5a43-4f11-bee6-6add0adf04d1","html_url":"https://github.com/segmentio/golines","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentio%2Fgolines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentio%2Fgolines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentio%2Fgolines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentio%2Fgolines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/segmentio","download_url":"https://codeload.github.com/segmentio/golines/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252915363,"owners_count":21824549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:01:35.384Z","updated_at":"2025-05-07T16:31:41.944Z","avatar_url":"https://github.com/segmentio.png","language":"Go","readme":"[![golines test](https://github.com/segmentio/golines/actions/workflows/test.yml/badge.svg)](https://github.com/segmentio/golines/actions/workflows/test.yml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/segmentio/golines)](https://goreportcard.com/report/github.com/segmentio/golines)\n[![GoDoc](https://godoc.org/github.com/segmentio/golines?status.svg)](https://godoc.org/github.com/segmentio/golines)\n\n# golines\n\nGolines is a Go code formatter that shortens long lines, in addition to all\nof the formatting fixes done by [`gofmt`](https://golang.org/cmd/gofmt/).\n\n## Motivation\n\nThe standard Go formatting tools (`gofmt`, `goimports`, etc.) are great, but\n[deliberately don't shorten long lines](https://github.com/golang/go/issues/11915);\ninstead, this is an activity left to developers.\n\nWhile there are different tastes when it comes to line lengths in go, we've generally found\nthat very long lines are more difficult to read than their shortened alternatives. As an example:\n\n```go\nmyMap := map[string]string{\"first key\": \"first value\", \"second key\": \"second value\", \"third key\": \"third value\", \"fourth key\": \"fourth value\", \"fifth key\": \"fifth value\"}\n```\n\nvs.\n\n```go\nmyMap := map[string]string{\n\t\"first key\": \"first value\",\n\t\"second key\": \"second value\",\n\t\"third key\": \"third value\",\n\t\"fourth key\": \"fourth value\",\n\t\"fifth key\": \"fifth value\",\n}\n```\n\nWe built `golines` to give Go developers the option to automatically shorten long lines, like\nthe one above, according to their preferences.\n\nMore background and technical details are available in\n[this blog post](https://yolken.net/blog/cleaner-go-code-golines).\n\n## Examples\n\nSee this [before](_fixtures/end_to_end.go) and [after](_fixtures/end_to_end__exp.go)\nview of a file with very long lines. More example pairs can be found in the\n[`_fixtures`](_fixtures) directory.\n\n## Version support\n\nNewer releases of `golines` require at least Go 1.18 due to generics-related dependencies.\nHowever, the [minimum version](https://go.dev/ref/mod#go-mod-file-go) in [`go.mod`](./go.mod)\nshould be considered the minimum required version of Go for any given version\nof `golines.` If you need to use `golines` with an older version of go, install\nthe tool from the `v0.9.0` release.\n\n## Usage\n\nFirst, install the tool. If you're using Go 1.21 or newer, run:\n\n```text\ngo install github.com/segmentio/golines@latest\n```\n\nOtherwise, for older Go versions, run:\n\n```text\ngo install github.com/segmentio/golines@v0.9.0\n```\n\nThen, run:\n\n```text\ngolines [paths to format]\n```\n\nThe paths can be either directories or individual files. If no paths are\nprovided, then input is taken from `stdin` (as with `gofmt`).\n\nBy default, the results are printed to `stdout`. To overwrite the existing\nfiles in place, use the `-w` flag.\n\n## Options\n\nSome other options are described in the sections below. Run `golines --help` to\nsee all available flags and settings.\n\n### Line length settings\n\nBy default, the tool tries to shorten lines that are longer than 100 columns\nand assumes that 1 tab = 4 columns. The latter can be changed via the\n`-m` and `-t` flags respectively.\n\n#### Dry-run mode\n\nRunning the tool with the `--dry-run` flag will show pretty, git-style diffs.\n\n#### Comment shortening\n\nShortening long comment lines is harder than shortening code because comments can\nhave arbitrary structure and format. `golines` includes some basic\nlogic for shortening single-line (i.e., `//`-prefixed) comments, but this is turned\noff by default since the quality isn't great. To enable this feature anyway, run\nwith the `--shorten-comments` flag.\n\n#### Custom formatters\n\nBy default, the tool will use [`goimports`](https://godoc.org/golang.org/x/tools/cmd/goimports)\nas the base formatter (if found), otherwise it will revert to `gofmt`. An explicit\nformatter can be set via the `--base-formatter` flag; the command provided here\nshould accept its input via `stdin` and write its output to `stdout`.\n\n#### Generated files\n\nBy default, the tool will not format any files that look like they're generated.\nIf you want to reformat these too, run with the flag `--ignore-generated=false`.\n\n#### Chained method splitting\n\nThere are several possible ways to split lines that are part of\n[method chains](https://en.wikipedia.org/wiki/Method_chaining). The original\napproach taken by `golines` was to split on the args, e.g.:\n\n```go\nmyObj.Method(\n\targ1,\n\targ2,\n\targ3,\n).AnotherMethod(\n\targ1,\n\targ2,\n).AThirdMethod(\n\targ1,\n\targ2,\n)\n```\n\nStarting in version 0.3.0, the tool now splits on the dots by default, e.g.:\n\n```go\nmyObj.Method(arg1, arg2, arg3).\n\tAnotherMethod(arg1, arg2).\n\tAThirdMethod(arg1, arg2)\n```\n\nThe original behavior can be used by running the tool with the\n`--no-chain-split-dots` flag.\n\n#### Struct tag reformatting\n\nIn addition to shortening long lines, the tool also aligns struct tag keys; see the\nassociated [before](_fixtures/struct_tags.go) and [after](_fixtures/struct_tags__exp.go)\nexamples in the `_fixtures` directory. To turn this behavior off, run with `--no-reformat-tags`.\n\n## Developer Tooling Integration\n\n### vim-go\n\nAdd the following lines to your vimrc, substituting `128` with your preferred line length:\n\n```vim\nlet g:go_fmt_command = \"golines\"\nlet g:go_fmt_options = {\n    \\ 'golines': '-m 128',\n    \\ }\n```\n\n### Visual Studio Code\n\n1. Install the [Run on Save](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave) extension\n2. Go into the VSCode settings menu, scroll down to the section for the \"Run on Save\"\n  extension, click the \"Edit in settings.json\" link\n3. Set the `emeraldwalk.runonsave` key as follows\n   (adding other flags to the `golines` command as desired):\n```\n    \"emeraldwalk.runonsave\": {\n        \"commands\": [\n            {\n                \"match\": \"\\\\.go$\",\n                \"cmd\": \"golines ${file} -w\"\n            }\n        ]\n    }\n```\n\n1. Save the settings and restart VSCode\n\n### Goland\n\n1. Go into the Goland settings and click \"Tools\" -\u003e \"File Watchers\" then click the plus to create a new file watcher\n2. Set the following properties:\n   - __Name:__ `golines`\n   - __File type:__ `Go files`\n   - __Scope:__ `Project Files`\n   - __Program:__ `golines`\n   - __Arguments:__ `$FilePath$ -w`\n   - __Output paths to refresh:__ `$FilePath$`\n3. In the \"Advanced Options\" section uncheck the __Auto-save edited files to trigger the watcher__ setting\n4. Confirm by clicking OK\n5. Activate your newly created file watcher in the Goland settings under \"Tools\" -\u003e \"Actions on save\"\n\n### Others\n\nComing soon.\n\n## How It Works\n\nFor each input source file, `golines` runs through the following process:\n\n1. Read the file, break it into lines\n2. Add a specially-formatted annotation (comment) to each line that's longer\n  than the configured maximum\n3. Use [Dave Brophy's](https://github.com/dave) excellent\n  [decorated syntax tree](https://github.com/dave/dst) library to parse the code\n  plus added annotations\n4. Do a depth-first traversal of the resulting tree, looking for nodes\n  that have an annotation on them\n5. If a node is part of a line that's too long, shorten it by altering\n  the newlines around the node and/or its children\n6. Repeat steps 2-5 until no more shortening can be done\n7. Run the base formatter (e.g., `gofmt`) over the results, write these to either\n  `stdout` or the source file\n\nSee [this blog post](https://yolken.net/blog/cleaner-go-code-golines) for more technical details.\n\n## Limitations\n\nThe tool has been tested on a variety of inputs, but it's not perfect. Among\nother examples, the handling of long lines in comments could be improved. If you see\nanything particularly egregious, please report via an issue.\n","funding_links":[],"categories":["Code Analysis","Code formatters","Go","代码分析","相关工具","Libraries for creating HTTP middlewares"],"sub_categories":["Routers","路由器","代码分析"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsegmentio%2Fgolines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsegmentio%2Fgolines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsegmentio%2Fgolines/lists"}