类型声明包括:常量、自定义类型、变量、函数、标签、还有包声明。
程序中的标识符都必须声明。块、包、文件中的标识符不能重复。
Declaration = ConstDecl | TypeDecl | VarDecl .
TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
标签是在标签语句中使用,常用在break、continue、goto语句中。定义了标签但不使用是非法的。与其他标识符相比,标签是非块作用域,不会与其他非标签标识符有冲突。
如下的标识符在golang中已保留
Types:
bool byte complex64 complex128 error float32 float64
int int8 int16 int32 int64 rune string
uint uint8 uint16 uint32 uint64 uintptr
Constants:
true false iota
Zero value:
nil
Functions:
append cap close complex copy delete imag len
make new panic print println real recover
一个标识符可导出让另一个包访问,只需要满足
用const 关键字
ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" )
ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ]
IdentifierList = identifier { "," identifier } .
ExpressionList = Expression { "," Expression } .
const Pi float64 = 3.14159265358979323846
const zero = 0.0 // untyped floating-point constant
const (
size int64 = 1024
eof = -1 // untyped integer constant
)
const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
const u, v float32 = 0, 3 // u = 0.0, v = 3.0
itoa表示连续的无类型的整形常量,从0开始计算,当又有itoa出现时,又重新置为0.
const ( // iota is reset to 0
c0 = iota // c0 == 0
c1 = iota // c1 == 1
c2 = iota // c2 == 2
)
const (
a = 1 << iota // a == 1 (iota has been reset)
b = 1 << iota // b == 2
c = 1 << iota // c == 4
)
const (
u = iota * 42 // u == 0 (untyped integer constant)
v float64 = iota * 42 // v == 42.0 (float64 constant)
w = iota * 42 // w == 84 (untyped integer constant)
)
const x = iota // x == 0 (iota has been reset)
const y = iota // y == 0 (iota has been reset)
用type
来定义一个新的类型
TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
TypeSpec = identifier Type .
type IntArray [16]int
type ( Point struct{ x, y float64 } Polar Point )
type TreeNode struct { left, right *TreeNode value *Comparable }
type Block interface { BlockSize() int Encrypt(src, dst []byte) Decrypt(src, dst []byte) }
自定义类型不会继承原有类型的方法,但接口方法或组合类型的元素则保留原有的方法。
// A Mutex is a data type with two methods, Lock and Unlock.
type Mutex struct { /* Mutex fields */ }
func (m *Mutex) Lock() { /* Lock implementation */ }
func (m *Mutex) Unlock() { /* Unlock implementation */ }
// NewMutex has the same composition as Mutex but its method set is empty.
type NewMutex Mutex
// The method set of the base type of PtrMutex remains unchanged,
// but the method set of PtrMutex is empty.
type PtrMutex *Mutex
// The method set of *PrintableMutex contains the methods
// Lock and Unlock bound to its anonymous field Mutex.
type PrintableMutex struct { Mutex }
// MyBlock is an interface type that has the same method set as Block.
type MyBlock Block
自定义类型可用于布尔、数值、或字符串类型,还可为其附加方法
type TimeZone int
const (
EST TimeZone = -(5 + iota)
CST
MST
PST
)
func (tz TimeZone) String() string {
return fmt.Sprintf("GMT%+dh", tz)
}
变量声明表示创建一个或多个变量,然后为之绑定相关类型,并赋之于初始值。
VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
var (
i int
u, v, s = 2.0, 3.0, "bar"
)
var re, im = complexSqrt(-1)
var _, found = entries[name] // map lookup; only interested in "found"
如果变量在声明但未使用,编译器会报错
短变量声明语法
ShortVarDecl = IdentifierList ":=" ExpressionList .
i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
r, w := os.Pipe(fd) // os.Pipe() returns two values
_, y, _ := coord(p) // coord() returns three values; only interested in y coordinate
但,短变量声明只能在函数内部,但在if、for、switch语句中可声明为临时变量。
语法如下
FunctionDecl = "func" FunctionName ( Function | Signature )
FunctionName = identifier
Function = Signature FunctionBody
FunctionBody = Block
方法是带接受者的函数。语法如下
MethodDecl = "func" Receiver MethodName ( Function | Signature )
Receiver = Parameters
接收者可以示T或*T类型
func (p *Point) Length() float64 {
return math.Sqrt(p.x * p.x + p.y * p.y)
}
func (p *Point) Scale(factor float64) {
p.x *= factor
p.y *= factor
}