目录
概述
new:new(T)分配了零值填充的T类型的内存空间,并且返回其地址,即一个*T类型的值。其自身是一个指针.可用于初始化任何类型
make: 返回一个有初始值(非零)的T类型,而不是*T,其只能用来初始化:slice,map和channel三种类型。
func make(t Type, size ...IntegerType) Type
slicemapchanreturn type
Slice
Map
Channel
func new(Type) *Type
the value returned is a pointer
代码示例
package main
import (
"fmt"
"reflect"
)
type Books struct {
Title,
Content,
Author string
}
func main() {
a := new([]int)
fmt.Println(a)
//输出&[],a本身是一个地址
b := make([]int, 1)
fmt.Println(b)
//输出[0],b本身是一个slice对象,其内容默认为0
}