<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### append 向原切片末尾添加元素,并返回新的切片對象. ~~~ s := []int{} fmt.Println(s, len(s), cap(s)) s = append(s, 1) s = append(s, 2) fmt.Println(s, len(s), cap(s)) s1 :=[]int{1,2,3} s1 = append(s1,100) fmt.Println(s1) ~~~ ~~~ [] 0 0 [1 2] 2 2 [1 2 3 100] ~~~ ### append擴容特點 函數 append 會智能地處理底層數組的容量增長。在切片的容量小于 1000 個元素時,總是 會成倍地增加容量。一旦元素個數超過 1000,容量的增長因子會設為 1.25,也就是會每次 增加 25%的容量。隨著語言的演化,這種增長算法可能會有所改變 ~~~ s := make([]int, 0, 1) fmt.Println(s, len(s), cap(s)) oldCap := cap(s) for i := 0; i < 17; i++ { s = append(s, i) if newCap := cap(s); oldCap < newCap { fmt.Println("當前長度", len(s),"新容量", newCap) } } ~~~ ~~~ [] 0 1 當前長度 2 新容量 2 當前長度 3 新容量 4 當前長度 4 新容量 4 當前長度 5 新容量 8 當前長度 6 新容量 8 當前長度 7 新容量 8 當前長度 8 新容量 8 當前長度 9 新容量 16 當前長度 10 新容量 16 當前長度 11 新容量 16 當前長度 12 新容量 16 當前長度 13 新容量 16 當前長度 14 新容量 16 當前長度 15 新容量 16 當前長度 16 新容量 16 當前長度 17 新容量 32 ~~~ **注意: 使用append向容量也使用了底層數組的切片添加元素也會改變底層數組的值** ~~~ slice := []int{10, 20, 30, 40, 50} newSlice := slice[1:3] fmt.Println(newSlice, len(newSlice), cap(newSlice)) newSlice = append(newSlice, 60) fmt.Println(newSlice, len(newSlice), cap(newSlice)) fmt.Println(slice, len(slice), cap(slice)) ~~~ ~~~ [20 30] 2 4 [20 30 60] 3 4 [10 20 30 60 50] 5 5 ~~~ ### 創建切片時的第三個索引 ~~~ 在創建切片時,還可以使用之前我們沒有提及的第三個索引選項。第三個索引可以用來控制新 切片的容量。其目的并不是要增加容量,而是要限制容量。可以看到,允許限制新切片的容量 為底層數組提供了一定的保護,可以更好地控制追加操作. *如果在創建切片時設置切片的容量 和長度一樣,就可以強制讓新切片的第一個 append 操作創建新的底層數組,與原有的底層數 組分離。新切片與原有的底層數組分離后,可以安全地進行后續修改.* ~~~ ~~~ slice := []int{10, 20, 30, 40, 50} newSlice := slice[1:3:3] fmt.Println(newSlice, len(newSlice), cap(newSlice)) newSlice = append(newSlice, 100) fmt.Println(slice, len(slice), cap(slice)) fmt.Println(newSlice, len(newSlice), cap(newSlice)) ~~~ **因為限制了容量,所以沒有對底層數組的元素也進行修改** ~~~ [20 30] 2 2 [10 20 30 40 50] 5 5 [20 30 100] 3 4 ~~~ ### ...運算符 使用...運算符可以將一個切片的所有元素添加到另一個元素當中 ~~~ s1 := []int{10, 20, 30, 40, 50} s2 := []int{90, 100} fmt.Println(append(s1, s2...)) ~~~ ~~~ [10 20 30 40 50 90 100] ~~~ ### copy ~~~ s1 := []int{1, 2, 3} s2 := []int{4, 5, 6, 7, 8, 9} copy(s2, s1) //前面是目標切片 fmt.Println(s2) ~~~ ~~~ [1 2 3 7 8 9] ~~~
                  <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>

                              哎呀哎呀视频在线观看