企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
* make和new的区别: * make只能用来分配及初始化类型为slice,map,chan的数据;new可以分配任意类型的数据 * new分配返回的是指针,即类型\*T;make返回引用,即T * new分配的空间被清零,make分配后,会进行初始化 * [http://docscn.studygolang.com/doc/effective\_go.html#make分配](http://docscn.studygolang.com/doc/effective_go.html#make%E5%88%86%E9%85%8D) * [http://docscn.studygolang.com/doc/effective\_go.html#new](http://docscn.studygolang.com/doc/effective_go.html#new)分配 > ### make ~~~ // The make built-in function allocates and initializes an object of type // slice, map, or chan (only). Like new, the first argument is a type, not a // value. Unlike new, make's return type is the same as the type of its // argument, not a pointer to it. The specification of the result depends on // the type: // Slice: The size specifies the length. The capacity of the slice is // equal to its length. A second integer argument may be provided to // specify a different capacity; it must be no smaller than the // length, so make([]int, 0, 10) allocates a slice of length 0 and // capacity 10. // Map: An empty map is allocated with enough space to hold the // specified number of elements. The size may be omitted, in which case // a small starting size is allocated. // Channel: The channel's buffer is initialized with the specified // buffer capacity. If zero, or the size is omitted, the channel is // unbuffered. func make(Type, size IntegerType) Type ~~~ * 内建函数 make 分配和初始化 一个 slice 或 map 或 chan 对象, 并且只能是这三种对象 * 和 new 类似,第一个参数也是一个类型而不是一个值, 不同的是 make 返回类型的引用而不是指针,而返回值也依赖于具体传入的类型 * slice : 第二个参数指定它的长度, 此时它的容量和长度相同. 可以用第三个参数来指定不同容量大小,但不能小于它的长度(第二个参数) * map : 根据size 大小来初始化分配内存,不过分配后的 map 长度为0。 如果 size 被忽略了,那么会在初始化分配内存的时候 分配一个小尺寸的内存 * channel : 管道缓冲区依据缓冲区容量被初始化。如果容量为 0 或者被忽略,管道是没有缓冲区的。 > ### len ~~~ // The len built-in function returns the length of v, according to its type: // Array: the number of elements in v. // Pointer to array: the number of elements in *v (even if v is nil). // Slice, or map: the number of elements in v; if v is nil, len(v) is zero. // String: the number of bytes in v. // Channel: the number of elements queued (unread) in the channel buffer; // if v is nil, len(v) is zero. func len(v Type) int ~~~ > ### cap ~~~ // The cap built-in function returns the capacity of v, according to its type: // Array: the number of elements in v (same as len(v)). // Pointer to array: the number of elements in *v (same as len(v)). // Slice: the maximum length the slice can reach when resliced; // if v is nil, cap(v) is zero. // Channel: the channel buffer capacity, in units of elements; // if v is nil, cap(v) is zero. func cap(v Type) int ~~~ * map变量被创建后,你可以指定map的容量,但是不可以在map上使用cap()方法 > ### new ~~~ // The new built-in function allocates memory. The first argument is a type, // not a value, and the value returned is a pointer to a newly // allocated zero value of that type. func new(Type) *Type ~~~ * 内建函数 new 用来分配内存,它的第一个参数是一个类型,不是一个值,它的返回值是一个指向新分配类型零值的指针 ~~~ package main import "fmt" func main() { number1 := [5]int{} number2 := new([5]int) fmt.Println(number1) fmt.Println(number2) } [0 0 0 0 0] &[0 0 0 0 0] package main import "fmt" type person struct { name string age int } func main() { p1 := person{} p2 := &person{} p3 := new(person) fmt.Println(p1) // 返回类型 fmt.Println(p2) // 返回指针 fmt.Println(p3) // 和p2一样 } { 0} &{ 0} &{ 0} ~~~ > ### nil ~~~ // nil is a predeclared identifier representing the zero value for a // pointer, channel, func, interface, map, or slice type. var nil Type // Type must be a pointer, channel, func, interface, map, or slice type package main import "fmt" func main() { // nil 是一个预定义标识符,其代表(用作)一些类型的零值;这些类型包括:pointer, channel, func, interface, map, slice var n1 []int var n2 map[int]string var n3 chan int if n1 == nil { fmt.Println("n1") } if n2 == nil { fmt.Println("n1") } if n3 == nil { fmt.Println("n1") } } ~~~ > ### 相关阅读 * [理解Go语言的nil](http://www.jianshu.com/p/dd80f6be7969) * [golang: 详解interface和nil](https://my.oschina.net/goal/blog/194233)