<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看