Go语言支持将相似的声明放在一个组内,例如:
Go
//bad usage import "a" import "b" //good usage import ( "a" "b" )
对于常量、变量和类型的声明同样如此:
//bad usage const a = 1 const b = 2 var a = 1 var b = 2 type Area float64 type Volume float64 //good usage const ( a = 1 b = 2 ) var ( a = 1 b = 2 ) type ( Area float64 Volume float64 )
不要将不相关的声明放于一组,例如:
//bad usage type Operation int const ( Add Operation = iota + 1 Subtract Multiply ENV_VAR = "MY_ENV" ) //good usage type Operation int const ( Add Operation = iota + 1 Subtract Multiply ) const ENV_VAR = "MY_ENV"
即使在函数中,也同样应该遵循该原则,例如:
//bad usage func f() string { var red = color.New(0xff0000) var green = color.New(0x00ff00) var blue = color.New(0x0000ff) ... } //good usage func f() string { var ( red = color.New(0xff0000) green = color.New(0x00ff00) blue = color.New(0x0000ff) ) ... }
Last updated 4 years ago
Was this helpful?