An article to help you understand the basics of Go language arrays
What is an array
My summary:A variable pointing to a blockContinuous, has length , and is of the same type A piece of memory.
How to define an array
var 变量名 [元素个数]元素类型
Example:
package main func main() { //声明一个name_list数组,长度为100,里面只能放字符串 var name_list [100]string }
Note:
var 变量名 [元素个数]元素类型 等同于 var 变量名 变量类型 所以 var name1 [3]int != var name2 [4]int 因为变量类型是不一样,不可以直接进行赋值
数组初始化
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] }
数组遍历
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) } }
多维数组
二维数组
通常情况下,二维数组基本够用,最多三维数组,再套娃就完犊子了。
定义一个二维数组
package main import "fmt" func main() { //定义一个三行两列的一个数组 var student_list = [3][2]string{ // 列 列 {"张三", "李四"}, //行 {"王五", "小刘"}, //行 {"小七", "王八"}, //行 } fmt.Println(student_list) }
循环二维数组
同理,定义一个二维数组需要两层,循环也需要两层。
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) } } }
多维数组是否可以长度推导
代码
package main import "fmt" func main() { //定义一个三行两列的一个数组 var student_list = [...][...]string{ // 列 列 {"张三", "李四"}, //行 {"王五", "小刘"}, //行 {"小七", "王八"}, //行 } fmt.Println(student_list) }
报错
似乎是不可以的,那我只用第一层试试呢。
package main import "fmt" func main() { //定义一个三行两列的一个数组 var student_list = [...][2]string{ // 列 列 {"张三", "李四"}, //行 {"王五", "小刘"}, //行 {"小七", "王八"}, //行 } fmt.Println(student_list) }
注:可以得出结论,在第一层时,是可以实现长度自动推导的。
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.
