当前位置:网站首页>go array
go array
2022-04-23 20:39:00 【baboon_ chen】
go array characteristic
1、 Arrays are value types , Assigning and passing copies the entire array ,⽽ It's not a pointer .
func printArray(x [2]int) {
fmt.Printf("the array x address is:%p\n", &x)
x[1] = 1000
}
func test1() {
var a [2]int = [2]int {
1}
fmt.Printf("the array a address is:%p\n", &a)
fmt.Printf("the value of a:%v\n", a)
printArray(a)
}
Output :
the array a address is:0xc0000a2070
the value of a:[1 0]
the array x address is:0xc0000a20a0
Value copy ⻉⾏ This will cause performance problems , It is usually suggested that ⽤ slice, Or array pointer .
2、 Array ⻓ Degree must be constant , And it's part of the type .[2]int and [3]int It's different types .
If the type is the same , Array ⽀ a “==”、"!=" The operator , Used to determine whether the values of the array are the same .
func test2() {
a := [2]int {
1,2}
b := [2]int {
1,2}
// error:non-constant array bound length
// var length int = 3
// c := [length]int {}
c := [3]int {
}
fmt.Printf("the type of a: %T, the value: %v\n", a, a)
fmt.Printf("the type of b: %T, the value: %v\n", b, b)
fmt.Printf("the type of c: %T, the value: %v\n", c, c)
if a == b {
fmt.Printf("a == b\n")
fmt.Printf("the address of a: %p\n", &a)
fmt.Printf("the address of b: %p\n", &b)
}
// error: invalid operation: a != c (mismatched types [2]int and [3]int)
// if a != c {
// fmt.Printf("a != c\n")
// fmt.Printf("the address of a: %p\n", &a)
// fmt.Printf("the address of c: %p\n", &c)
// }
}
Output :
he type of a: [2]int, the value: [1 2]
the type of b: [2]int, the value: [1 2]
the type of c: [3]int, the value: [0 0 0]
a == b
the address of a: 0xc000012090
the address of b: 0xc0000120a0
3、 Pointer array [n]*T, Array pointer *[n]T.
4、 Initialization of an array
func test3() {
a := [3]int {
1,2} // Uninitialized element value is 0.
b := [...]int {
1,2,3,4} // The array is determined by the initialization value ⻓ degree .
c := [5]int {
2:10, 4:100} // send ⽤ The index number initializes the element .
d := [...]int {
3:100, 5:100} // The index can be used to determine the length of the array
e := [...]struct {
name string
age int
} {
{
"chen",25}, // Element types can be omitted .
{
"yang",25}, // Don't forget the last ⼀⾏ Comma of .
}
fmt.Printf("the type of a: %T, the value: %v\n", a, a)
fmt.Printf("the type of b: %T, the value: %v\n", b, b)
fmt.Printf("the type of c: %T, the value: %v\n", c, c)
fmt.Printf("the type of d: %T, the value: %v\n", d, d)
fmt.Printf("the type of e: %T, the value: %v\n", e, e)
}
Output :
the type of a: [3]int, the value: [1 2 0]
the type of b: [4]int, the value: [1 2 3 4]
the type of c: [5]int, the value: [0 0 10 0 100]
the type of d: [6]int, the value: [0 0 0 100 0 100]
the type of e: [2]struct {
name string; age int }, the value: [{
chen 25} {
yang 25}]
5、 Multidimensional arrays , First determine the number of columns , Then determine the number of rows .
func test4() {
a := [2][3]int {
{
1,1,1}, {
2,2,2}}
b := [...][2]int {
// The first 2 Latitude cannot ⽤ "...".
{
1,1},
{
2,2},
{
3,3},
}
fmt.Printf("the type of a: %T, the value: %v\n", a, a)
fmt.Printf("the type of b: %T, the value: %v\n", b, b)
}
Output :
the type of a: [2][3]int, the value: [[1 1 1] [2 2 2]]
the type of b: [3][2]int, the value: [[1 1] [2 2] [3 3]]
6、 The length of the array .
func test5() {
a := [2]int {
}
fmt.Println("the len of a: ", len(a)," the cap of a: ", cap(a))
}
Output :
the len of a: 2 the cap of a: 2
Built in functions len and cap Can return an array ⻓ degree ( Element quantity ).
版权声明
本文为[baboon_ chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210546351471.html
边栏推荐
- Leetcode 709, convert to lowercase
- Browser - learning notes
- A useless confession artifact
- Numpy Index & slice & iteration
- Monte Carlo py solves the area problem! (save pupils Series)
- Some basic configurations in interlij idea
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
- Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
- ArcGIS js api 4. X submergence analysis and water submergence analysis
- LeetCode 1346、检查整数及其两倍数是否存在
猜你喜欢
A useless confession artifact
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
Linux64Bit下安装MySQL5.6-不能修改root密码
Unity solves Z-fighting
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
The ODB model calculates the data and outputs it to excel
Resolve the error - error identifier 'attr_ id‘ is not in camel case camelcase
After route link navigation, the sub page does not display the navigation style problem
JS arrow function user and processing method of converting arrow function into ordinary function
随机推荐
Case of the third day of go language development fresh every day project - news release system II
LeetCode 994、腐烂的橘子
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
Solve the Chinese garbled code of URL in JS - decoding
Leetcode 709, convert to lowercase
6-5 string - 2 String copy (assignment) (10 points) the C language standard function library includes the strcpy function for string copy (assignment). As an exercise, we write a function with the sam
Recognition of high-speed road signs by Matlab using alexnet
Monte Carlo py solves the area problem! (save pupils Series)
"Meta function" of tidb 6.0: what is placement rules in SQL?
What is the difference between a host and a server?
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行
Leetcode 74. Search two-dimensional matrix
【PTA】L1-006 连续因子
Livego + ffmpeg + RTMP + flvjs to realize live video
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
Unity Odin ProgressBar add value column
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
The ODB model calculates the data and outputs it to excel
SQL: query duplicate data and delete duplicate data