类型(续)

指针类型

nil
PointerType = "*" BaseType .
BaseType    = Type .

*Point
*[4]int

函数类型

nil
FunctionType   = "func" Signature .
Signature      = Parameters [ Result ] .
Result         = Parameters | Type .
Parameters     = "(" [ ParameterList [ "," ] ] ")" .
ParameterList  = ParameterDecl { "," ParameterDecl } .
ParameterDecl  = [ IdentifierList ] [ "..." ] Type .
...
func()
func(x int) int
func(a,_ int,z float32) bool
func(a,b int,z float32) (bool)
func(prefix ,values ...int)
func(a,z float64,opt ...interface{}) (success bool)
func(int,int,float64) (float64,*[]int)
func(n int) func(p *T)

接口类型

nil
InterfaCEType   = "interface" "{" { MethodSpec ";" } "}" 
MethodSpec       = MethodName Signature | InterfaCETypename 
MethodName         = identifier
InterfaCETypename  = Typename

接口的所有@L_616_5@必须是唯一的非空白名称

// A simple FilE interface
interface {
    Read(b Buffer) bool
    Write(b Buffer) bool
    Close()
}

任意的一个或多个类型都可实现此接口,例如T类型

func (p T) Read(b Buffer) bool { return … }
func (p T) Write(b Buffer) bool { return … }
func (p T) Close() { … }

空类型不包含任何@L_616_5@,因此所有的类型都实现了空接口

interface{}

接口可以嵌套
接口T可以使用接口E作为嵌入字段,则T隐式的包含E的所有@L_616_5@

type Locker interface { Lock() Unlock() }
type File interface { ReadWriter // same as adding the @H_393_189@methods of ReadWriter Locker // same as adding the @H_393_189@methods of Locker Close() }

切记:接口不可循环嵌套

// illegal: Bad cAnnot embed itself
type Bad interface { Bad }

@H_610_2@map(K-V)类型

@H_742_9@map类型表示K类型到V类型的映射,未初始化的Map类型默认为
nil
@H_487_249@mapType = "map" "[" KeyType "]" ElementType .
KeyType = Type .
==panic
@H_964_48@map[]int
@H_964_48@map[*T]struct{ x,y float64 }
@H_964_48@map[]interface{}
len
@H_997_302@make(@H_964_48@map[]int)
@H_997_302@make(@H_964_48@map[]int, 100)

管道(ChAnnel)类型

nil
ChAnnelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType
<-
chan T  // can be used to send and receive values of type T
chan<- float64  // can only be used to send float64s
<-chan int      // can only be used to receivE ints

使用@H_184_10@make @L_616_5@来创建管道

@H_997_302@make(chan int, 100)

最后一个参数表示容量,可以省略。定义了该参数,表示此管道是一个缓冲管道,因此该管道只能存储指定的大小的元素,当超出长度后,则会阻塞,直到goroutIne从管道中读取一些元素,腾出空间。

close