不同的Go类型的零值(没有显式初始化的默认值)是什么?

huangapple go评论74阅读模式
英文:

What are the zero values (default values without explicit initialization) for different Go types?

问题

Go语言为没有显式初始化的变量提供默认值,也称为零值。内置类型的不同零值是什么?

英文:

Go gives default values, or zero values, for variables that are declared without an explicit initialization. What are the different zero values for builtin types?

答案1

得分: 1

  • 布尔值:false
  • 整数:0
  • 浮点数:0.0
  • 字符串:""
  • 指针:nil
  • 函数:nil
  • 接口:nil
  • 切片:nil
  • 通道:nil
  • 映射:nil

这是递归完成的,因此数组和结构体(以及结构体数组等)的未初始化值将被设置为上述零值。

当使用new和make为变量分配存储空间时,也适用这一规则。

来源:http://golang.org/ref/spec#The_zero_value

英文:
  • Booleans: false
  • Integers: 0
  • Floats: 0.0
  • Strings: ""
  • Pointers: nil
  • Functions: nil
  • Interfaces: nil
  • Slices: nil
  • Channels: nil
  • Maps: nil

This is done recursively, so arrays and structs (and arrays of structs etc.) will have their uninitialized values set to the zero values described above.

This also applies when using new and make to allocate storage for variables.

Source: http://golang.org/ref/spec#The_zero_value

huangapple
  • 本文由 发表于 2015年1月10日 22:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/27877268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定