An article to help you understand the basics of Go language arrays

Release: 2023-07-19 16:40:23
forward
924 people have browsed it

What is an array

My summary:A variable pointing to a blockContinuous, has length , and is of the same type A piece of memory.

An article to help you understand the basics of Go language arrays

How to define an array

var 变量名 [元素个数]元素类型
Copy after login

Example:

package main

func main() {
    //声明一个name_list数组,长度为100,里面只能放字符串
    var name_list [100]string
}
Copy after login

Note:

##
var 变量名 [元素个数]元素类型 等同于 var 变量名 变量类型
所以
var name1 [3]int != var name2 [4]int
因为变量类型是不一样,不可以直接进行赋值
Copy after login

An article to help you understand the basics of Go language arrays

数组初始化

package main

import "fmt"

func main() {
    //方式一,声明不赋值
    //var name_list [10]int
    //fmt.Println(name_list) //结果:[0 0 0 0 0 0 0 0 0 0] 声明不赋值,int类型默认是0,其他类型也有默认值
    //
    //方式二, 声明没有赋值完
    //var name_list [10]int = [10]int{1, 3}
    //fmt.Println(name_list) //结果:[1 3 0 0 0 0 0 0 0 0],没有赋值完的,其他仍然是默认值

    //方式三,声明完完全赋值
    //var name_list = [3]int{1, 6, 10} //使用类型推断方式,同上
    //fmt.Println(name_list)           //结果:[1 6 10],每个都有值,没啥可说的

    //方式四,自动推断个数
    //var name_list = [...]int{1, 2, 4, 5, 19} //...表示自动推断个数,不会存在过多或者过少
    //fmt.Println(name_list)                   //结果:[1 2 4 5 19]

    //方式五,指定索引方式赋值,用的很少
    var name_list = [...]int{1: 66, 4: 11} //下标1赋值为66,下标4赋值11,其他默认值
    fmt.Println(name_list)                 //结果:[0 66 0 0 11]
}
Copy after login

数组遍历

package main

import "fmt"

func main() {
    var name_list = [...]string{"张三", "李四", "王五", "小刘"}
    //方式一,普通for遍历
    //for i := 0; i < len(name_list); i++ {
    //fmt.Println(name_list[i])
    //}

    //方式二,for range方式
    for index, name := range name_list {
        //index是每个的下标,name是值
        fmt.Println(index, name)
  }
}
Copy after login

多维数组

二维数组

通常情况下,二维数组基本够用,最多三维数组,再套娃就完犊子了。

定义一个二维数组

package main

import "fmt"

func main() {
    //定义一个三行两列的一个数组
    var student_list = [3][2]string{
        // 列     列
        {"张三", "李四"}, //行
        {"王五", "小刘"}, //行
        {"小七", "王八"}, //行
}
    fmt.Println(student_list)
}
Copy after login

循环二维数组

同理,定义一个二维数组需要两层,循环也需要两层。

package main

import "fmt"

func main() {
    //定义一个三行两列的一个数组
    var student_list = [3][2]string{
        // 列     列
        {"张三", "李四"}, //行
        {"王五", "小刘"}, //行
        {"小七", "王八"}, //行
}
    //方式一,普通for循环
    //for i := 0; i < len(student_list); i++ {
    ////fmt.Println(student_list[i])//每行
    ///*
    //      [张三 李四]
    //      [王五 小刘]
    //      [小七 王八]
    //*/
    //for j := 0; j < len(student_list[i]); j++ {
    //      //每列
    //      fmt.Println(student_list[i][j])
    //}
    //}

    //方式二,for range
    for _, v := range student_list {
        //fmt.Println(v) //每行
        /*
            [张三 李四]
            [王五 小刘]
            [小七 王八]
        */
        for _, b := range v {
            //每列
            fmt.Println(b)
        }
  }
}
Copy after login

多维数组是否可以长度推导

代码

package main

import "fmt"

func main() {
    //定义一个三行两列的一个数组
    var student_list = [...][...]string{
        // 列     列
        {"张三", "李四"}, //行
        {"王五", "小刘"}, //行
        {"小七", "王八"}, //行
}
    fmt.Println(student_list)
}
Copy after login

报错

An article to help you understand the basics of Go language arrays

似乎是不可以的,那我只用第一层试试呢。

package main

import "fmt"

func main() {
  //定义一个三行两列的一个数组
  var student_list = [...][2]string{
    // 列     列
    {"张三", "李四"}, //行
    {"王五", "小刘"}, //行
    {"小七", "王八"}, //行
  }
  fmt.Println(student_list)
}
Copy after login

An article to help you understand the basics of Go language arrays

注:可以得出结论,在第一层时,是可以实现长度自动推导的。

The above is the detailed content of An article to help you understand the basics of Go language arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template