In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the basic array of Go? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Array of Go Basics
Array (array)
In Go, an array is determined from the time it is declared, and its members can be modified when it is used, but the size of the array is immutable.
Var array variable name [number of elements] T
/ / define an array of length 3 elements of type int a
Vara [3] int
The length of the array must be constant and the length must be part of the array type. Once defined, the length cannot be changed. Int and [5] int are different types.
The array can be accessed through the subscript, starting with 0, and the last element subscript is: len-1. If the access is out of bounds (the subscript is outside the legal range), the access will be panic.
Initialization of array
Mode one
You can use the initialization list to set the value of an array element when initializing an array
Funcmain () {
VarArray1 [3] int
VarArray2= [3] int {1,2}
VarArray3= [3] string {"Beijing", "Shanghai", "Guangzhou"}
Fmt.Println (Array1)
Fmt.Println (Array2)
Fmt.Println (Array3)
}
/ / [000]
/ / [120]
/ / [Beijing, Shanghai and Guangzhou]
Mode two
Follow the above method to make sure that the initial value provided is consistent with the length of the array each time. In general, we can let the compiler infer the length of the array based on the number of initial values.
Funcmain () {
VarArray1 [3] int
VarArray2= [...] int {1,2}
VarArray3= [...] string {"Beijing", "Shanghai", "Guangzhou"}
Fmt.Println (Array1)
Fmt.Println (Array2)
Fmt.Printf ("typeofArray2:%T\ n", Array2)
Fmt.Println (Array3)
Fmt.Printf ("typeofArray3:%T\ n", Array3)
}
/ / [000]
/ / [12]
/ / typeofArray2: [2] int
/ / [Beijing, Shanghai and Guangzhou]
/ / typeofArray3: [3] string
Mode 3
Initializes the array by specifying an index value
Funcmain () {
AVOR = [...] int {1DV 1JI 3JR 5}
Fmt.Println (a)
Fmt.Printf ("typeofa:%T\ n", a)
}
/ / [0105]
/ / typeofa: [4] int
Traversal of array
Funcmain () {
A string = [...] Beijing, Shanghai, Guangzhou}
/ / method 1: for loop traversal
Fori:=0;i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.