<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## 切片類型( slice) 1.切片的創建方式 基于底層數組創建,直接創建,或者 make() 函數創建 1. 與數組相比切片的長度是不固定的,可以追加元素,在追加時可能使切片的容量增大。會進行分配內存和 copy 操作 1. len() 方法獲取長度。 3. 添加切片 `append` 與拷貝切片 `copy` ``` //創建 a :=[]int {10,20,30,40,50,60,70} b :=a[1:3] //{20,30} c :=a[1:] //{20,30,40,50,60,70} d :=a[:3] //{10,20,30} //添加貼片 var numbers =[]int{1,2,3} /* 允許追加空切片 */ numbers = append(numbers, 3) fmt.Println(numbers) //[1 2 3 3] /* 拷貝 numbers 的內容到 numbers1 */ copy(numbers1,numbers) //len=3 cap=8 slice=[0 1 2] ``` > 區別 ``` a :=[]int{1,2,3} //切片 b :=[...]int{1,2,3} //數組 c :=b[:] //從數組中生成切片 fmt.Println(reflect.TypeOf(a))//[]int fmt.Println(reflect.TypeOf(b)) //[3]int fmt.Println(reflect.TypeOf(c)) //[]int -切片 值為[1 2 3] ``` 切片傳值 > [參考文章](https://studygolang.com/articles/4810) ``` func main() { args :=make([]string,0) args=append(args,"1") demo(args) fmt.Printf("%+v\n", args) // [1] demo1(args) fmt.Printf("%+v\n", args) // [-1] } // 在切片中追加,無效 func demo(args []string){ args=append(args,"2") } // 可修改已有切片的值 func demo1(args []string){ args[0]="-1" args=nil // 賦值nil 也無效 } ``` ### make([]int) 指定的長度根據真實長度擴展 ``` a := make([]int, 2, 3) fmt.Println(len(a)) //2 fmt.Println(cap(a)) //3 a = append(a, 2, 2, 2) fmt.Println(len(a)) //5 fmt.Println(cap(a)) //6 ``` ### 切片內存技巧 返回值可以為切片的復用 ``` func TrimSpace(s []byte) []byte { b := s[:0] for _, x := range s { if x != ' ' { b = append(b, x) } } return b } ``` ### 避免切片內存泄漏 切片操作并不會復制底層的數據。底層的數組會被保存在內存中,直到它不再被引用。但是有時候可能會因為一個小的內存引用而導致底層整個數組處于被使用的狀態,這會延遲自動內存回收器對底層數組的回收 bad ``` func FindPhoneNumber(filename string) []byte { b, _ := ioutil.ReadFile(filename) return regexp.MustCompile("[0-9]+").Find(b) } ``` good: ``` func FindPhoneNumber(filename string) []byte { b, _ := ioutil.ReadFile(filename) b = regexp.MustCompile("[0-9]+").Find(b) return append([]byte{}, b...) } ``` ## 操作 ### copy ``` a := []int{1, 2, 3, 4} b := make([]int, len(a)) copy(b, a) fmt.Printf("%+v\n", b) // [1 2 3 4] ``` ### Append ``` a=append(a,b...) ``` * 當 append 之后的長度小于等于 cap,將會直接利用原底層數組剩余的空間。 * 當 append 后的長度大于 cap 時,則會分配一塊更大的區域來容納新的底層數組。 > 因此,為了避免內存發生拷貝,如果能夠知道最終的切片的大小,預先設置 cap 的值能夠獲得最好的性能。 ### Delete 切片的底層是數組,因此刪除意味著后面的元素需要逐個向前移位 方式一 ``` a := []int{1, 2, 3, 4} index := 2 // 刪除第三個 a = append(a[:index], a[index+1:]...) fmt.Printf("%+v\n", a) // [1,2,4] ``` 方式二: ``` a := []int{1, 2, 3, 4} index := 2 // 刪除第三個 a = a[:index+copy(a[index:], a[index+1:])] fmt.Printf("%+v\n", a) // [1,2,4] ``` ### Delete(GC) 刪除后,將空余的位置置空,有助于垃圾回收 ``` a := []int{1, 2, 3, 4} index := 2 // 刪除第三個 copy(a[index:], a[index+1:]) a[len(a)-1] = 0 // 置空 zero or nil a = a[:len(a)-1] fmt.Printf("%+v\n", a) // [1,2,4] ``` ### Insert ``` a := []int{1, 2, 3, 4} index := 2 // 刪除第三個 a = append(a[:index], append([]int{9}, a[index+1:]...)...) fmt.Printf("%+v\n", a) // [1,2,9,4] ``` 即在某個位置添加一個元素后,將該位置后面的元素再 append 回去。復雜度為 O(N)。因此,不適合大量隨機插入的場景 ### Push 在末尾追加元素,不考慮內存拷貝的情況,復雜度為 O(1) ``` a=append(a,x) ``` 在頭部追加元素,時間和空間復雜度均為 O(N),不推薦。 ``` a := []int{1, 2, 3, 4} a = append([]int{9}, a...) fmt.Printf("%+v\n", a) // [9 1 2 3 4] ``` ### Pop 尾部刪除 ``` a = a[:len(a)-1] ``` 頭部刪除 ``` a = a[1:] ```
                  <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>

                              哎呀哎呀视频在线观看