Golang的fmt不是格式括号吗?

ext*_*xts 3 go

所以我一直在尝试再次使用golang,我记得go有一个格式化工具来清理你的源代码.所以当我运行go fmt我的项目时,以下错误会被吐出:

expected declaration, found '{'

我期待fmt修复我的括号以尊重go的语法要求,但它只是吐出一个错误.这是预期的功能还是应该将括号放在正确的行上(与函数声明相同的行)?

我希望格式正确的基本代码:

package main

func main()
{
    println("Learning go again")
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*hin 8

Go对有效代码是什么有一些非常强烈的意见.
这是其中一个案例.与许多其他C系列语言不同,它允许将函数体声明的大括号放在同一行或下一行(如果你喜欢的话,甚至是后面的十行),Go编译器允许它只放在同一行上.

另一个例子是else语句.用其他语言在哪里

if {
}
else {
}
Run Code Online (Sandbox Code Playgroud)

可能是有效的甚至是首选的,在Go中仅编译else语句的形式

if {
} else {
}
Run Code Online (Sandbox Code Playgroud)

go fmt将修复空或缺少空格但:

func main()             {
    fmt.Println("Hello, playground")
}

func main(){
   fmt.Println("Hello, playground")
}
Run Code Online (Sandbox Code Playgroud)

两者都会成为

func main() {
   fmt.Println("Hello, playground")
}
Run Code Online (Sandbox Code Playgroud)