声明一致性

一、相似声明放于一组

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
)

二、相关声明放于一组

不要将不相关的声明放于一组,例如:

即使在函数中,也同样应该遵循该原则,例如:

Last updated

Was this helpful?