26

Go源码剖析:内置类型

 5 years ago
source link: https://studygolang.com/articles/18148?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Go内置类型定义在$GOROOT/src/builtin/builtin.go中,分为内置函数和内置数据类型

  1. 内置数据类型

    • 简单类型

      整型:int,int8,int16,int32,int64
      无符号整型:uint,uint8,uint16,uint32,uint64
      浮点型:float32,float64
      复数:complex64,complex128
      byte,rune,iota,uintptr
    • 复杂类型

      数组 array
      切片 slice
      映射 map
      管道 chan
  2. 内置函数

    • append()

      定义:func append(slice []Type, elems ...Type) []Type

      作用:往切片末尾添加新元素,如果切片的容量不够,会自定创建新切片

      用法:

      1. slice = append(slice, elem1, elem2)//依次添加多个元素
         2. slice = append(slice, anotherSlice...)//批量添加所有元素
         3. slice = append([]byte("hello"),"world")//特例,允许字符串添加在字节切片后面
    • copy()

      定义;func copy(dst, src []Type) int

      作用:把源切片拷贝到目标变量,返回拷贝的元素数量

    • copy()
      定义;func copy(dst, src []Type) int
      作用:把源切片拷贝到目标变量,返回拷贝的元素数量
    • delete()
      定义;func delete(m map[Type]Type1, key Type)
      作用:根据key,删除映射中的一个元素
    • len()
      定义;func len(v Type) int
      作用:返回数组,切片,channel的长度
    • cap()

      定义;func cap(v Type) int

      作用:返回数组,切片,channel的容量

    • make()
      定义;func make(t Type, size ...IntegerType) Type
      作用:分配然后初始化指定的(切片,映射,管道),并返回该值
    • new()
      定义;func new(Type) *Type
      作用:分配指定类型的地址空间,返回指针
    • complex(),real(),imag()
      作用:comples()构造一个复数,real()返回实部,imag()返回虚部
    • close()

      定义;func close(c chan<- Type)

      作用:关闭一个双向或者send-only的管道,

    • panic(),recover()

      定义;

      func panic(v interface{})
        作用:终止goroutine运行,并立即调用该函数内的defer
        func recover() interface{}
        作用:需要定义在defer里面
    • print(),println()
      作用:打印信息

3.总结

Go语言的内置类型就是以上所有,有任何问题的话可以直接参考源码文件:https://golang.org/src/builtin/builtin.go

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK