类型声明包括:常量、自定义类型、变量、函数、标签、还有包声明。
程序中的标识符都必须声明。块、包、文件中的标识符不能重复。
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
const (
size int64 = 1024
eof = -1
)
const a, b, c = 3, 4, "foo"
const u, v float32 = 0, 3
Itoa(枚举)
itoa表示连续的无类型的整形常量,从0开始计算,当又有itoa出现时,又重新置为0.
const (
c0 = iota
c1 = iota
c2 = iota
)
const (
a = 1 <<iota
b = 1 <<iota
c = 1 <<iota
)
const (
u = iota * 42
v float64 = iota * 42
w = iota * 42
)
const x = iota
const y = iota
类型声明
用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]
如果变量在声明但未使用,编译器会报错
短变量声明
短变量声明语法
ShortVarDecl = IdentifierList ":=" ExpressionList .
i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
r, w := os.Pipe(fd)
_, y, _ := coord(p)
但,短变量声明只能在函数内部,但在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
}