gofmt 命令不格式化花括号 ({})。
代码:
package main
import "fmt"
func main()
{
fmt.Printf("Hello, world\n")
}
gofmt hello.go 的输出
hello.go:6:1: 预期声明,找到“{” 退出状态2
如果我把空格放在任何地方,它就会正确格式化。 gofmt 不会格式化大括号吗?
最佳答案
你必须把花括号放在函数声明的同一行,否则你的代码将因为这个语法错误而无法编译
Why are there braces but no semicolons? And why can't I put the opening brace on the next line?
Go uses brace brackets for statement grouping, a syntax familiar to programmers who have worked with any language in the C family. Semicolons, however, are for parsers, not for people, and we wanted to eliminate them as much as possible. To achieve this goal, Go borrows a trick from BCPL: the semicolons that separate statements are in the formal grammar but are injected automatically, without lookahead, by the lexer at the end of any line that could be the end of a statement. This works very well in practice but has the effect that it forces a brace style. For instance, the opening brace of a function cannot appear on a line by itself.
关于GoLang gofmt 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520011/