哪种方式是在Go中声明单个常量的首选方式?
1)
2)
1)
const myConst
2)
const (
myConst
)
两种方式都被gofmt
接受。这两种方式都可以在stdlib中找到,但是第一种方式更常用。
const myConst
const (
myConst
)
两种方式都被gofmt
接受。这两种方式都可以在stdlib中找到,但是第一种方式更常用。
archive/tar/reader.go
。const maxNanoSecondIntSize = 9
// Compression methods.
const (
Store uint16 = 0
Deflate uint16 = 8
)
const()
中:当你有通过iota(连续整数)初始化的常量时,每个块都会计算。cmd/yacc/yacc.go
。// flags for state generation
const (
DONE = iota
MUSTDO
MUSTLOOKAHEAD
)
// flags for a rule having an action, and being reduced
const (
ACTFLAG = 1 << (iota + 2)
REDFLAG
)
这是正确的,但你会发现iota只用于常量声明,如果你需要多组连续的整数常量,那么就必须定义多个const()
块。
const ()
块的示例。 - VonC