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
1
vote
2 answers

How to use rewrite rule to replace tabs with spaces in gofmt tool?

gofmt command supports -r flag to use rewrite rule during formatting the code. How to use it to replace tabs indentation with the spaces indentation?
Psylone
  • 2,788
  • 1
  • 18
  • 15
1
vote
2 answers

need help while tesing println and fmt.println of golang

I wrote simple codes to test println and fmt.Println, but when I ran the code, it printed different results almost everytime. I tried to google the difference between println and fmt.Println but got nothing. Is there any one who knows the real…
sillydong
  • 61
  • 1
  • 5
1
vote
1 answer

Any tools rebase a branch in Git while running go fmt each commit separately?

I wish to rebuild/rebase all commits in a Git branch X using a source code formatting tool like go fmt or indent. I'd expect the workflow to roughly consist of making a new branch off master and iterating the following with $_ ranging over the…
Jeff Burdges
  • 4,204
  • 23
  • 46
0
votes
1 answer

Golang: How do I use fmt.Fscanf() & fmt.Sscanf() correctly?

I'm working on a program that is supposed to read the contents of a file and extract the data from it. I've tried to use fmt.Fscanf() to scan the contents line by line, but for some reason, I can't get it to work correctly. The entire line gets…
0
votes
1 answer

Problem with formatting go code in vscode editor

In a fresh VSCode 1.52.1, I have installed Go extention by Go Team at Google v0.19.1 as well as the suggested go packages but the auto format at save does not work as it should. go version go1.14.6 linux/amd64 In settings.json I have { …
hjiw
  • 13
  • 6
0
votes
2 answers

Is it possible to augment fmt.print with default indentation level?

I am messing around creating modules and interlinking them. When testing out behaviours I am calling packages that call other packages. Is there an easy way to modify behaviour of fmt package with indentation level of future calls. So that parent…
GoTTimw
  • 2,330
  • 6
  • 26
  • 34
0
votes
1 answer

Golang custom type fmt printing

I have a custom type like this: type Timestamp struct { Time time.Time } // some more methods... Now when I print an instance of it using fmt: test := Timestamp{ Time: time.Now(), } fmt.Println("TEST:", test) the output is: TEST:…
Techradar
  • 3,506
  • 3
  • 18
  • 28
0
votes
0 answers

How do I propose a change to Go formatting?

The documentation for Go's format package says: Note that formatting of Go source code changes over time. How does that happen? I have a specific minor change that I'd like to run past the community. To be clear, I'm delighted that the community…
Scott Deerwester
  • 3,503
  • 4
  • 33
  • 56
0
votes
1 answer

go generate stdout piped to gofmt to file

What is the syntax so go generate can pipe stdout from go run to gofmt and ultimately to a file? Below is simple example of what I have tried. Its in the file main.go. I can't find any examples of this after searching. Thank you. Edit:…
user2133814
  • 2,431
  • 1
  • 24
  • 34
0
votes
1 answer

Error while printing selected fields after parsing JSON Object

Trying to parse a JSON object via Go and iterate through the records and print selected fields only. Facing issue as unable to reference while printing using fmt. JSON Object Structure: type myBalanceInfo struct { Name string …
Lastwish
  • 317
  • 10
  • 21
0
votes
0 answers

Practical approach to pretty print vs. string conversion

I'd like to trivially provide a mechanism for logging data using pretty prints rather than plain type->string conversions which doesn't interfere with data transfer through strings. I can add a type.String() converter method - which will then…
Mordachai
  • 9,412
  • 6
  • 60
  • 112
0
votes
1 answer

Go fmt producing inconsistently formatted results for math expressions

My understanding is that go fmt is supposed to produce code that is readable and consistently formatted. However I'm not finding this to be the case. I typed in the commented code and go fmt returned the uncommented code. Why did it collapse…
asynth
  • 214
  • 2
  • 8
0
votes
2 answers

Terminal input doesn't match expected result

I am new to Go. I wrote the program below to allow a user to enter the name. package main import ( "bufio" "fmt" "os" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter name:") name, err :=…
Nixon Kosgei
  • 788
  • 8
  • 13
0
votes
1 answer

How to print backslash in go?

I have the following code snippet: const byte1 = 0x19; const byte2 = 0x45; msg := fmt.Sprintf("\\x%x\\x%x message", byte1, byte2) log.Info("Learning go fmt", "msg", msg) I get this: msg="\\x19\\x45 message" Why is the backslash duplicated?…
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
0
votes
1 answer

In go, why are both the reflected value and its interface the same when printed?

Excerpt from the Laws of Reflection: (Why not fmt.Println(v)? Because v is a reflect.Value; we want the concrete value it holds.) This confuses me because the following code: var x float64 = 3.4 var v = reflect.ValueOf(x) fmt.Println("value of…
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114