GoLang gofmt 命令

标签 go gofmt

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.

https://golang.org/doc/faq#semicolons

关于GoLang gofmt 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520011/

相关文章:

转到 fmt float64 问题

arrays - 不能在赋值 : need type assertion 中使用字(类型接口(interface) {})作为类型字符串

memory-management - bytes.Buffer 是否执行大量重新分配?

go - c :=make(chan int) and c:=make(chan int, 1)有什么区别?

bash - 当 gofmt 建议更改时,让 gofmt 以退出状态 1 退出?

go - 如何格式化包含动态数量元素的字符串?

postgresql - 如何设计数据库服务类

go - cgo如何在c中表示go类型?

go - 终端输入与预期结果不符