1.指针
1.1.常规定义
go
func test24() {
var a int = 10
var b *int
b = &a
fmt.Printf("a 的 值%d\n", a)
fmt.Printf("a 的 指针地址%x\n", &a)
fmt.Printf("b 的 值%d\n", *b)
fmt.Printf("b 的 指针地址%x\n", b)打印
var c *string
if c == nil {
println("c is null")
} else {
println("c is not null")
}
}
结果:
go
a 的 值10
a 的 指针地址c0001140d0
b 的 值10
b 的 指针地址c0001140d0
c is null
说明 :
如果变量类型为指针,使用*b
来获取值
如果类型直接是指针, 使用&b
获取地址
打印的时候, 使用%x
打印地址, 使用%d
打印数字,使用%s打印字符串
1.2.指针数组
go
func test25() {
var d = [3]int{1, 2, 3}
var e [3]*int
for i := 0; i < len(d); i++ {
e[i] = &d[i]
}
for i := 0; i < len(d); i++ {
fmt.Printf("d[%d] = %d\n", i, *e[i])
}
}
结果:
go
d[0] = 1
d[1] = 2
d[2] = 3
1.3.指针的指针
go
func test26() {
var a int = 10
var b *int
var c **int
b = &a
c = &b
fmt.Printf("b = %d\n", *b)
fmt.Printf("c = %d\n", **c)
}
结果:
go
b = 10
c = 10
说明 :
b为指针, 所以获取值的时候, 使用*b
获取
c为指针的指针, 获取值的时候, 使用**c
获取
1.4.指针形参
go
func test27() {
var a = 10
var b = 20
c := test27V1(&a, &b)
fmt.Printf("c = %d\n", c)
}
func test27V1(a *int, b *int) int {
return *a * *b
}
结果:
go
c = 200
2.结构体(实体类)
go
type StructA struct {
param1 int
param2 string
param3 bool
param4 StructB
param5 *int
param6 *StructB
}
type StructB struct {
param1 int
}
func test28() {
var a StructA
a.param1 = 1
a.param2 = "a"
a.param3 = true
var b StructB
b.param1 = 10
a.param4 = b
c := 20
a.param5 = &c
var d StructB
d.param1 = 30
a.param6 = &d
fmt.Printf("param1 value is [%d]\n", a.param1)
fmt.Printf("param2 value is [%s]\n", a.param2)
fmt.Printf("param3 value is [%t]\n", a.param3)
fmt.Printf("param4 value is [%v, %+v, %#v]\n", a.param4, a.param4, a.param4)
fmt.Printf("param5 value is [%p, %#p]\n", a.param5, a.param5)
fmt.Printf("param1 value is [%d]\n", a.param6.param1)
}
结果:
go
param1 value is [1]
param2 value is [a]
param3 value is [true]
param4 value is [{10}, {param1:10}, main.StructB{param1:10}]
param5 value is [0xc0000a60d8, c0000a60d8]
param1 value is [30]
3.Map
3.1.Map值的获取
go
func test33() {
m := make(map[string]int)
m["key1"] = 1
m["key2"] = 2
m["key3"] = 3
fmt.Printf("key1's value is %d\n", m["key1"])
fmt.Printf("m's length is %d\n", len(m))
key4, status := m["key4"]
if status {
fmt.Println("key4 value is ", key4)
} else {
fmt.Println("key4 is not exist.")
}
}
结果:
go
key1's value is 1
m's length is 3
key4 is not exist.
说明 :
直接获取map的值的时候, 返回的第二个参数为:是否存在, bool类型
3.2.Map的遍历
go
func test33() {
m := make(map[string]int)
m["key1"] = 1
m["key2"] = 2
m["key3"] = 3
for key, value := range m {
fmt.Printf("1--->key is %s, value is %d\n", key, value)
}
n := map[string]string{"a": "value1", "b": "value2", "c": "value3"}
for key, value := range n {
fmt.Printf("2--->key is %s, value is %s\n", key, value)
}
}
结果:
go
1--->key1's value is 1
1--->key is key3, value is 3
1--->key is key1, value is 1
1--->key is key2, value is 2
2--->key is c, value is value3
2--->key is a, value is value1
2--->key is b, value is value2
说明 :
map 遍历的时候, 顺序不是固定的
3.3.删除和清空
go
func test34() {
n := map[string]string{"a": "value1", "b": "value2", "c": "value3"}
delete(n, "a")
for key, value := range n {
fmt.Printf("key is %s, value is %s\n", key, value)
}
clear(n)
fmt.Println("n map: ", n)
}
结果:
go
key is b, value is value2
key is c, value is value3
n map: map[]
3.4.判断两个map是否相等
go
package main
import (
"fmt"
"maps"
)
func test35() {
m := map[string]string{"a": "value1", "b": "value2", "c": "value3"}
n := map[string]string{"a": "value1", "b": "value2", "c": "value3"}
if maps.Equal(m, n) {
fmt.Println("m == n")
}
}