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
2
votes
1 answer

How to format a string containing a dynamic number of elements?

I am trying to format a string based on the elements received from the function calling it. This number of elements can varyfrom one to many. Is there a way to call fmt.Sprintf with a variable number of elements. Something along the lines…
2
votes
1 answer

Is there a way to prevent gofmt from converting code functions to multiline without disabling gofmt entirely?

I'm using Vim version 8.1.1401 with go version go1.13.1 linux/amd64. I'm trying to disable gofmt from putting all if statement brackets on a new line, for instance: if thing { return } is converted to if thing { return } The only way I've…
darthguinea
  • 115
  • 3
  • 7
2
votes
3 answers

how can I take input from user in Golang (fmt.scan)

I can not take input from the user in Golang by use fmt.scan(). package main import "fmt" func main() { fmt.Print("Enter text: ") var input string e, _ := fmt.Scanln(&input) fmt.Println(input) fmt.Println(e) } image of…
mohammad ghari
  • 153
  • 2
  • 13
2
votes
1 answer

How to set inital value of text input in Golang?

I know that you can call for text input from the user in go by doing the following: fmt.Print("Enter text: ") reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n') Which would ouput: Enter text: But is there a way to set an…
Grant Pauker
  • 21
  • 1
  • 1
2
votes
1 answer

Does my gofmt work wrongly or I don't understand something?

I suppose that my gofmt works not how it's supposed to, am I right ? Original file: package main import "fmt" func main() { fmt.Printf("hello, world\n") } Then I did: gofmt -r 'h -> H' -w "hello.go" Content of the file after: package…
Andy D
  • 125
  • 13
2
votes
1 answer

golang gofmt package rewrite wildcard

I'm trying to do a gofmt rewrite of all packages that start with a certain prefix. Something like: gofmt -r 'github.com/some/path/ -> someotherrepo.com/some/path/' Obviously wildcard isn't valid syntax, just showing the concept.…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
2
votes
1 answer

Go fmt float64 issue

I've come across a bit of a gotcha in my noob understanding of go fmt package It relates to the following code: import "fmt" func main() { var smallFloat float64 = 0.123456789 var bigFloat float64 = 123456789000 …
Airomega
  • 574
  • 6
  • 22
1
vote
1 answer

"rune literal not terminated" error when attempting to run gofmt -r with exec.Command

In the following directory structure, . ├── foo.go ├── go.mod └── main.go I have a foo.go with a simple type definition: package main type Foo struct { Baz string } If I run gofmt -r from the command line to replace a variable name, it…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
1
vote
1 answer

How do you feed string input to bufio.Scanner and fmt.Scanln sequentially?

Here's a simple program using bufio.Scanner and fmt.Scanln that works as expected: scanner := bufio.NewScanner(os.Stdin) var s string fmt.Println("Enter a string:") scanner.Scan() s = scanner.Text() fmt.Printf("You entered: %q\n\n", s) var i…
Savantes
  • 103
  • 1
  • 7
1
vote
1 answer

Spit out code formatting errors than actually formatiing in golang

I am working on a large go codebase and go code doesn't seem to be properly formatted. I want to know if there is an option in go that will in some way assert me that what places I need to change the code styling. I am not using any IDE. go fmt does…
user1079065
  • 2,085
  • 9
  • 30
  • 53
1
vote
1 answer

vscode-go input tabs of size 4 instead 8

I have Visual Studio Code Insiders installed. Basically I've installed Go extension from Microsoft and written simple hello world app. Now I always use spaces to indent my code, but I saw that it changed to tabs of size 4. So I thought: yay, what a…
dabljues
  • 1,663
  • 3
  • 14
  • 30
1
vote
1 answer

Define string output for type in Golang

I'm wondering if there's a way through fmt to specify the way a string would get outputted for specific types. For example, I have a token struct that contains a bunch of information on the token, like token type (which is an int, but for clarity…
Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
1
vote
2 answers

Go equivalent of C's negated scansets

What is the way to mimick the negated scansets that exist in C? For an example input string: aaaa, bbbb In go using: fmt.Sscanf(input, "%s, %s", &str1, &str2) The result is only str1 being set as: aaaa, In C one could use a format string as…
João Almeida
  • 4,487
  • 2
  • 19
  • 35
1
vote
2 answers

How to expand variables with fmt.Println()

I can't expand variables with fmt.Println(). package main import "fmt" func main(){ old := 20 fmt.Println("I'm %g years old.",old) } result => I'm %g years old. 20
Junya Kono
  • 1,121
  • 3
  • 10
  • 16
1
vote
3 answers

GoLang gofmt command

gofmt command does not format the flower braces({}). Code: package main import "fmt" func main() { fmt.Printf("Hello, world\n") } Output of gofmt hello.go hello.go:6:1: expected declaration, found '{' exit status 2 If i put the spaces…
Chirag Pahuja
  • 31
  • 1
  • 6