Questions tagged [gofmt]

Go command. Gofmt formats Go programs. It uses tabs (width = 8) for indentation and blanks for alignment.

Go command. Gofmt formats Go programs. It uses tabs (width = 8) for indentation and blanks for alignment.

Without an explicit path, it processes the standard input. Given a file, it operates on that file; given a directory, it operates on all .go files in that directory, recursively. (Files starting with a period are ignored.) By default, gofmt prints the reformatted sources to standard output.

Useful links

59 questions
173
votes
2 answers

Indentation in Go: tabs or spaces?

Is there a standard Google Go coding conventions document somewhere that sets whether tabs or spaces are preferred for indentation in Go source code? What is the official recommendation, if any?
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
14
votes
5 answers

how to split long lines for fmt.sprintf

I have a very long line in fmt.Sprintf. How do I split it in the code? I don't want to put everything in a single line so the code looks ugly. fmt.Sprintf("a:%s, b:%s ...... this goes really long")
WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30
10
votes
2 answers

File is not `gofmt`-ed with `-s`: why is this happening and how to resolve it?

We use a linter (for Golang) that run through a Github Actions workflow every time we open or update a Pull Request on our repository. It recently started to return the following error: File is not `gofmt`-ed with `-s` (gofmt) After what happened in…
GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
9
votes
1 answer

Can you call gofmt to format a file you've written from inside the Go code that wrote it?

I'm writing Go code that outputs other Go code. I'd like to know if there's a way to call the gofmt tool to format the code I've written from within the code that's done the writing. The documentation I've found on gofmt, e.g. the official docs,…
R. Duke
  • 363
  • 1
  • 3
  • 14
8
votes
2 answers

Runtime error when parsing JSON array and map elements with trailing commas using Go

Dave Cheney, one of the leading subject matter experts on Go, wrote: "When initializing a variable with a composite literal, Go requires that each line of the composite literal end with a comma, even the last line of your declaration. This is the…
John Difool
  • 5,572
  • 5
  • 45
  • 80
6
votes
2 answers

GOPATH not found error from the Gofmt plugin within SublimeText

I keep getting this error within Sublime Text, after installing the gofmt package: Traceback (most recent call last): File "/Users/abrahma/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 257, in…
agam
  • 5,064
  • 6
  • 31
  • 37
5
votes
2 answers

Make gofmt exit with exit status 1, when gofmt suggests changes?

I would like to add gofmt to the CI/CD pipeline. If it produces changes, I want gofmt to exit with status 1. For example if I run gofmt -s -l . and there are some files listed. I want it to exit with status 1. Right now when I run echo $? gives me…
Vasantha Ganesh
  • 4,570
  • 3
  • 25
  • 33
5
votes
1 answer

Emacs Golang gofmt on save hook - not formatting

after checking path with M-: (eval "PATH") the location of C:/Go/bin shows up, so I know the gofmt binary is found. If i try to do M-x gofmt on other files it will give proper errors (not *.go files). however I noticed something weird in the…
Andrei
  • 1,196
  • 1
  • 11
  • 25
5
votes
1 answer

Inconsistent formatting of Go code?

Here are some example code: func main() { os.MkdirAll(outDir + id, 0755) os.Create(outDir + id + "/txt") os.OpenFile(outDir + id + "/" + ".tmp", os.OWRONLY|os_APPEND) os.Stat(outDir + id + "/.tmp") } The following is the output…
John Smith
  • 381
  • 1
  • 5
  • 12
4
votes
2 answers

Run gofmt on vim without plugin

I want to run gofmt on save on vim without installing any plugin. This is what I have tried (inspired by https://gist.github.com/tbrisbout/a91ac3419440cde40c5f54dc32c94427): function! GoFmt() let file = expand('%') silent execute "!gofmt -w " .…
hidetatz
  • 195
  • 8
4
votes
3 answers

Run goimports on save in Sublime Text?

In Sublime Text 3, with plugins GoSublime and GoImports installed. I'm having trouble having goimports run on my file automatically everytime I save. Here's what I tried : My GoSublime settings are set to : { "env": {"GOPATH":…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
4
votes
2 answers

go generate with gofmt, replacing variable value

The release of the 'generate' tool opens up a whole lot of exciting possibilities. I've been trying to make my tests better. I have a function which queries an external API, the location of that API is defined in a global variable. One piece of the…
harm
  • 10,045
  • 10
  • 36
  • 41
3
votes
0 answers

Vscode golang extention takes long time on format

These are the configs on which I run the vscode and golang: go version go1.14.1 linux/amd64 vscode version 1.43.2 ms-codego version 0.13.1 Linux/amd64 When I want to save a file with go extension it takes a long time on formatting Saving…
Emad Helmi
  • 652
  • 8
  • 25
3
votes
0 answers

Correct way to use a Go project on Windows, with Gofmt mangling CRLF as it does

According to this closed (works as expected) go issue 2242 the expected behaviour of gofmt on Windows is to change all file line endings for you to cr+lf. The behaviour on Posix systems is reverse. With my git repo set to core.autocrlf true, or…
Warren P
  • 65,725
  • 40
  • 181
  • 316
2
votes
2 answers

How to programmatically retrieve the package path where a given type is declared?

I am looking for a way to retrieve the package(s) installed locally which contain the declaration for a given type and the default package name. ie: // FindPackagesForType returns the list of possible packages for a given type func…
Coyote
  • 2,454
  • 26
  • 47
1
2 3 4