引⽤用类型包括 slice、map 和 channel。它们有复杂的内部结构,除了申请内存外,还需

要初始化相关属性。

内置函数 new 计算类型⼤大⼩小,为其分配零值内存,返回指针。⽽而 make 会被编译器翻译

成具体的创建函数,由其分配内存和初始化成员结构,返回对象⽽而⾮非指针。

a := []int{0, 0, 0} // 提供初始化表达式。
a[1] = 10
b := make([]int, 3) // makeslice
b[1] = 10
c := new([]int)
c[1] = 10 // Error: invalid operation: c[1] (index of type *[]int)


不⽀支持隐式类型转换,即便是从窄向宽转换也不⾏行。


var b byte = 100
// var n int = b // Error: cannot use b (type byte) as type int in assignment
var n int = int(b) // 显式转换



使⽤用括号避免优先级错误。


*Point(p) // 相当于 *(Point(p))
(*Point)(p)
<-chan int(c) // 相当于 <-(chan int(c))
(<-chan int)(c)


同样不能将其他类型当 bool 值使⽤用。


a := 100
if a { // Error: non-bool a (type int) used as if condition
println("true")
}