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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### 切片 > **slice [開始位置:結束位置]** > len() 獲取當前切片長度(實際存在元素個數),cap()函數獲取當前切片容量(起始元素開始到底層數組中最后一個元素的個數),& 獲取地址(內存地址) ``` package main import "fmt" func main() { var student = [...]string{"Tom", "Ben", "Peter"} var student1 = student[1:2] var student2 = student[:] fmt.Println("student數組", student) // student數組 [Tom Ben Peter] fmt.Println("student1切片", student1) //student1切片 [Ben] fmt.Println("student2切片", student2) // student1切片 [Tom Ben Peter] fmt.Println("student[1]的地址", &student[1]) // student[1]的地址 0xc000076490 fmt.Println("student1[0]的地址", &student1[0]) // student1[0]的地址 0xc000076490 fmt.Println("student1切片長度", len(student1)) // student1切片長度 1 fmt.Println("student1切片容量", cap(student1)) // student1切片容量 2 } ``` ### 聲明切片 > **var 切片變量名 []元素類型** ``` package main import "fmt" func main() { var student []int fmt.Println("student切片", student) fmt.Println("student切片長度", len(student)) fmt.Println("student切片容量", cap(student)) fmt.Println("判斷student切片是否為空", student == nil) } 結果: student切片 [] student切片長度 0 student切片容量 0 判斷student切片是否為空 true ``` ### 初始化切片 > **make([]元素類型, 切片長度, 切片容量)** > **make([]元素類型, 長度容量一樣)** > 容量必須大于長度 > 或者使用賦值的方法初始化 ``` package main import "fmt" func main() { var student []int student = make([]int, 2, 10) fmt.Println("student切片", student) fmt.Println("student切片長度", len(student)) fmt.Println("student切片容量", cap(student)) fmt.Println("判斷student切片是否為空", student == nil) } 結果: student切片 [0 0] student切片長度 2 student切片容量 10 判斷student切片是否為空 false ``` ``` package main import "fmt" func main() { var student = []int{1, 2} fmt.Println("student切片", student) fmt.Println("student切片長度", len(student)) fmt.Println("student切片容量", cap(student)) fmt.Println("判斷student切片是否為空", student == nil) } 結果: student切片 [1 2] student切片長度 2 student切片容量 2 判斷student切片是否為空 false ``` ### 為切片添加元素 > **使用append()函數添加元素,當切片長度等于容量值時(不能容納其他元素),再次使用appedn()添加元素,容量會按照2倍數擴充** ``` package main import "fmt" func main() { student := make([]int, 0, 1) fmt.Println("添加前student切片", student) for i := 0; i < 5; i++ { student = append(student, i) fmt.Println("當前切片長度", len(student), "當前切片容量", cap(student)) } fmt.Println("添加后student切片", student) var str = [...]string{"Tom", "Ben", "Peter"} str1 := str[0:1] fmt.Println("str數組", str) fmt.Println("str1切片", str1) str1 = append(str1, "Pony") //添加的元素會覆蓋原數組位置元素 fmt.Println("添加Pony后str1切片,長度,容量", str1, len(str1), cap(str1)) fmt.Println("添加Pony后的str數組", str) } 結果: 添加前student切片 [] 當前切片長度 1 當前切片容量 1 當前切片長度 2 當前切片容量 2 當前切片長度 3 當前切片容量 4 當前切片長度 4 當前切片容量 4 當前切片長度 5 當前切片容量 8 添加后student切片 [0 1 2 3 4] str數組 [Tom Ben Peter] str1切片 [Tom] 添加Pony后str1切片,長度,容量 [Tom Pony] 2 3 添加Pony后的str數組 [Tom Pony Peter] ``` ### 從切片中刪除元素 > **Go語言沒有刪除切片元素的方法,需要手動將刪除點前后元素連接起來** ``` package main import "fmt" func main() { var student = []string{"Tom", "Ben", "Peter", "John"} fmt.Println("刪除Ben前,長度,容量", student, len(student), cap(student)) student = append(student[0:1], student[2:]...) //等價于 student = append(student[0:1], student[2], student[3]) fmt.Println("刪除Ben后,長度,容量", student, len(student), cap(student)) student = student[0:0] fmt.Println("清空后,長度,容量", student, len(student), cap(student)) } 結果: 刪除Ben前,長度,容量 [Tom Ben Peter John] 4 4 刪除Ben后,長度,容量 [Tom Peter John] 3 4 清空后,長度,容量 [] 0 4 ``` ### 遍歷切片 > **遍歷切片的方法 類似于遍歷數組。k是索引,v是切片副本元素** ``` for k,v := range 容器變量{ fmt.Println("索引",k,"元素",v) } ``` ``` package main import "fmt" func main() { var student = []string{"Tom", "Ben", "Peter", "John"} for k, v := range student { fmt.Println("索引", k, "元素", v) } } 結果: 索引 0 元素 Tom 索引 1 元素 Ben 索引 2 元素 Peter 索引 3 元素 John ```
                  <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>

                              哎呀哎呀视频在线观看