Go data type-2
Pointer type
- Go pointers and C++ pointers are in the reverse order, see the example:
package main
import "fmt"
func main() {
param := 100
var lp *int = ¶m
fmt.Println("data = ", *lp)
}
- Go's *int is C++'s int*, Go's **int is C++'s int**
- The difference between Go pointers and C++ is:
1. Go的指针没有->运算符, 通过.来调用
2. Go的空指针是如下定义的 —— var lp *int = nil
- The scope in Go is similar to C++, if, functions, etc. are all a scope, which is different from python, so the pointer type can change the value across the scope, which is also the advantage of the pointer type
Array
- Initialization of one-dimensional array
var arr_1 [3]int = [3]int{1, 2, 3}
// 前后必须对应, 前面是[3], 后面也是[3],
- Two-dimensional array initialization:
arr := [1][2]int{
{1, 2}}
var arr [1][2]int
arr = [1][2]int{
{1, 2}}
// 未初始化的元素全部被设置成0
- The only comparison operators for arrays are == and! =, only exactly the same is ==
- Go's array is passed by value as a function parameter, which is different from C++, so if you want to change it in a function, you need to pass a pointer
- Go's array pointer is similar to:
arr := [3]int{1, 2, 3}
var lp *[3]int = &arr
fmt.Printf("%T", lp)
Note:
The [] of the array and the array pointer must correspond, 3 is written on it, and 3 must also be written for the array pointer
slice
- Slices are very similar to arrays, the specific len written in [] is the array, and the empty is the slice
- The way the slice is created:
arr := []int{1, 2, 3}
arr := make([]int, 2, 5) // 2是len, 5是cap容量
arr := make([]int, 2)
- The capacity of the slice must be >= the length of the slice
- The slices generated from the array will affect the underlying array
package main
import "fmt"
func main() {
arr := [3]int{1, 2, 3}
s := arr[0:2] // 截取[0, 2)的切片, 容量会由系统为你指定也可以自己写
s[0] = 10
fmt.Println(arr)
// 这时候就变成了10 2 3
}
- Append function use-is to add new elements at the end of the original slice
package main
import "fmt"
func main() {
s := []int{}
s = append(s, 1)
fmt.Println(s)
}
- The copy function, as its name suggests, is to copy elements
package main
import "fmt"
func main() {
s1 := []int{1, 1}
s2 := []int{3, 3, 3}
copy(s2, s1) // 把s1的元素拷贝到s2, 最后的结果为1, 1, 3
fmt.Println(s2)
}
- Slice as a function parameter is not passed by value, it will affect the original slice
package main
import "fmt"
func change(s []int) {
s[0] = 10
}
func main() {
s1 := []int{1, 1}
change(s1)
fmt.Println(s1)// 结果是10 1
}
map
- Go's map can be understood by analogy with C++'s map and python's dict
- The creation of the map is as follows-[] is the key, followed by the value
var mp1 map[int]int
mp2 := make(map[int]int) // 此处可以指定初始的容量cap, 但是一旦元素len超过cap会自动扩容
mp3 := map[int]int{1: 1, 2: 2}
- The key of the map is unique, and the key cannot be a variable with reference semantics such as a slice. It must support == and! =
- The default assignment operation of map is to create a key-value pair
- When map accesses a key that does not exist, it will return empty or 0 similar v data
- Delete elements of the map:
package main
import "fmt"
func main() {
mp := map[int]string{1: "1", 2: "2"}
delete(mp, 1) // 删除mp之中key为1的键值对
fmt.Println(mp)
}
- Map has reference semantics, see the following example:
package main
import "fmt"
func f(mp map[int]string) {
delete(mp, 1)
}
func main() {
mp := map[int]string{1: "1", 2: "2"}
f(mp)
fmt.Println(mp)
}
Structure
- The definition of the structure, renamed with type
type Person struct {
age int
name string
sex bool
}
Note:
The variables in it do not need to write var
- Initialization of the structure:
// 完全初始化, 必须写完整
var person Person = Person{10, "xiaohua", true}
// 部分初始化, 剩下的自动填充为0
person1 := Person{age:15}
- Structure pointer
package main
import "fmt"
type Person struct {
age int
name string
sex bool
}
func main() {
var person *Person = &Person{10, "xiaohua", true}
fmt.Println(*person)
person1 := &Person{age:15}
fmt.Println(*person1)
}
- The internal members of structure variables and variable pointer operations are all operated by.
package main
import "fmt"
type Person struct {
age int
name string
sex bool
}
func main() {
var person *Person = &Person{10, "xiaohua", true}
person.age = 20 // 等价于(*person).age = 20
fmt.Println(*person)
}
- Structure as a function parameter is passed by value