美文网首页
go标准库: builtin

go标准库: builtin

作者: PhilicX | 来源:发表于2020-03-25 01:10 被阅读0次

    builtin中包含了一些go语言中内置的类型,常量,函数等;它们实际被实现在语言内部,src/builtin/builtin.go可作为文档使用

    类型

    type bool bool
    const (
        true  = 0 == 0 // Untyped bool.
        false = 0 != 0 // Untyped bool.
    )
    type uint8 uint8
    type uint16 uint16
    type uint32 uint32
    type uint64 uint64
    type int8 int8
    type int16 int16
    type int32 int32
    type int64 int64
    type float32 float32
    type float64 float64
    type complex64 complex64
    type complex128 complex128
    type string string
    type int int
    type uint uint
    type uintptr uintptr  // 指针类型
    type byte = uint8
    type rune = int32
    

    方法

    func append(slice []Type, elems ...Type) []Type
    
    • 注意如果原始slice的容量不足以装下新元素,会自动开辟一段新的内存
    func copy(dst, src []Type) int
    
    • dst和src两段内存是可以有重叠部分的
    func delete(m map[Type]Type1, key Type)
    
    • 如果key不存在于m中或者m为nil,此方法为无效操作,但不会报错
    func len(v Type) int
    func cap(v Type) int
    
    • 对于Channel而言,len()返回了chan中的元素实际个数,cap()返回buffer的容量大小
    func make(t Type, size ...IntegerType) Type
    func new(Type) *Type
    
    • make用来构造slice,map,chan等,返回的是对象本身
    • new返回的是对象的指针
    • channel只能用make来构建
    func complex(r, i FloatType) ComplexType
    func real(c ComplexType) FloatType
    func imag(c ComplexType) FloatType
    
    • 复数相关
    func close(c chan<- Type)
    
    • 关闭channel,永远只能由发送方调用
    func panic(v interface{})
    func recover() interface{}
    
    • panic()会终止当前goroutine的正常运行,并开始依次执行defer修饰的语句;panic会从callee传递到caller,直到goroutine完全终止;如果整个程序因此终止,会返回非零值
    • 如果在defer修饰的语句中调用了recover,它可以终止panic的传播并返回传入panic(v)的参数
    func print(args ...Type)
    func println(args ...Type)
    
    • 不同于fmt中的print函数,此处定义的是系统内置的print,在以后版本可能不再支持

    接口

    type error interface {
        Error() string
    }
    

    相关文章

      网友评论

          本文标题:go标准库: builtin

          本文链接:https://www.haomeiwen.com/subject/semzyhtx.html