<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Go 切片 切片是Go語言的關鍵類型之一,它提供了比數組更多的功能。 示例1: ```go package main import "fmt" func main() { // 和數組不同的是,切片的長度是可變的。 // 我們可以使用內置函數make來創建一個長度不為零的切片 // 這里我們創建了一個長度為3,存儲字符串的切片,切片元素 // 默認為零值,對于字符串就是""。 s := make([]string, 3) fmt.Println("emp:", s) // 可以使用和數組一樣的方法來設置元素值或獲取元素值 s[0] = "a" s[1] = "b" s[2] = "c" fmt.Println("set:", s) fmt.Println("get:", s[2]) // 可以用內置函數len獲取切片的長度 fmt.Println("len:", len(s)) // 切片還擁有一些數組所沒有的功能。 // 例如我們可以使用內置函數append給切片追加值,然后 // 返回一個擁有新切片元素的切片。 // 注意append函數不會改變原切片,而是生成了一個新切片, // 我們需要用原來的切片來接收這個新切片 s = append(s, "d") s = append(s, "e", "f") fmt.Println("apd:", s) // 另外我們還可以從一個切片拷貝元素到另一個切片 // 下面的例子就是創建了一個和切片s長度相同的新切片 // 然后使用內置的copy函數來拷貝s的元素到c中。 c := make([]string, len(s)) copy(c, s) fmt.Println("cpy:", c) // 切片還支持一個取切片的操作 "slice[low:high]" // 獲取的新切片包含元素"slice[low]",但是不包含"slice[high]" // 下面的例子就是取一個新切片,元素包括"s[2]","s[3]","s[4]"。 l := s[2:5] fmt.Println("sl1:", l) // 如果省略low,默認從0開始,不包括"slice[high]"元素 l = s[:5] fmt.Println("sl2:", l) // 如果省略high,默認為len(slice),包括"slice[low]"元素 l = s[2:] fmt.Println("sl3:", l) // 我們可以同時聲明和初始化一個切片 t := []string{"g", "h", "i"} fmt.Println("dcl:", t) // 我們也可以創建多維切片,和數組不同的是,切片元素的長度也是可變的。 twoD := make([][]int, 3) for i := 0; i < 3; i++ { innerLen := i + 1 twoD[i] = make([]int, innerLen) for j := 0; j < innerLen; j++ { twoD[i][j] = i + j } } fmt.Println("2d: ", twoD) } ``` 輸出結果為 ``` emp: [ ] set: [a b c] get: c len: 3 apd: [a b c d e f] cpy: [a b c d e f] sl1: [c d e] sl2: [a b c d e] sl3: [c d e f] dcl: [g h i] 2d: [[0] [1 2] [2 3 4]] ``` 數組和切片的定義方式的區別在于`[]`之中是否有`固定長度`或者推斷長度標志符`...`。 示例2: ```go package main import "fmt" func main() { s1 := make([]int, 0) test(s1) fmt.Println(s1) } func test(s []int) { s = append(s, 3) //因為原來分配的空間不夠,所以在另外一個地址又重新分配了空間,所以原始地址的數據沒有變 } ``` 輸出結果為: ``` [] ``` 若改為: ```go package main import "fmt" func main() { s1 := make([]int, 0) s1 = test(s1) fmt.Println(s1) } func test(s []int) []int { s = append(s, 3) return s } ``` 輸出結果為: ``` [3]//正確結果 ``` 示例3: cap是slice的最大容量,append函數添加元素,如果超過原始slice的容量,會重新分配底層數組。 ``` package main import "fmt" func main() { s1 := make([]int, 3, 6) fmt.Println("s1= ", s1, len(s1), cap(s1)) s2 := append(s1, 1, 2, 3) fmt.Println("s1= ", s1, len(s1), cap(s1)) fmt.Println("s2= ", s2, len(s2), cap(s2)) s3 := append(s2, 4, 5, 6) fmt.Println("s1= ", s1, len(s1), cap(s1)) fmt.Println("s2= ", s2, len(s2), cap(s2)) fmt.Println("s3= ", s3, len(s3), cap(s3)) } ``` 輸出結果為: ``` s1= [0 0 0] 3 6 s1= [0 0 0] 3 6 s2= [0 0 0 1 2 3] 6 6 s1= [0 0 0] 3 6 s2= [0 0 0 1 2 3] 6 6 s3= [0 0 0 1 2 3 4 5 6] 9 12 ``` 示例4: 指向同一底層數組的slice之間copy時,允許存在重疊。copy數組時,受限于src和dst數組的長度最小值。 ```go package main import "fmt" func main() { s1 := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s2 := make([]int, 3, 20) var n int n = copy(s2, s1) fmt.Println(n, s2, len(s2), cap(s2)) s3 := s1[4:6] fmt.Println(n, s3, len(s3), cap(s3)) n = copy(s3, s1[1:5]) fmt.Println(n, s3, len(s3), cap(s3)) } ``` 輸出結果: ``` 3 [0 1 2] 3 20 3 [4 5] 2 6 2 [1 2] 2 6 ```
                  <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>

                              哎呀哎呀视频在线观看