-
Notifications
You must be signed in to change notification settings - Fork 15
/
valueandpoint.go
148 lines (136 loc) · 3.13 KB
/
valueandpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
@author: Jason Pang
@desc: Go值和指针
@date: 2021/11/21
**/
package various
import (
"encoding/json"
"fmt"
"unsafe"
)
type ImageItem struct {
Key string `thrift:"key,1" json:"key"`
Name string `thrift:"name,2" json:"name"`
FileType string `thrift:"fileType,3" json:"fileType"`
}
type Test struct {
Hello string
World string
}
//错误示例
func ErrorShow() {
fmt.Println("------------------错误示例")
var a, b string = "a", "b"
ai := ImageItem{
Key: a,
Name: a,
FileType: a,
}
bi := ImageItem{
Key: b,
Name: b,
FileType: b,
}
//news
newS := make(map[int]*string)
newL := []ImageItem{ai, bi}
for k, item := range newL {
fmt.Println("------------------第", k+1, "次循环")
fmt.Printf("item的地址为 %p\n", &item)
fmt.Println("item的值为", item)
fmt.Printf("新key的地址为%p,老key的地址为%p\n", &item.Key, &newL[k].Key)
newS[k] = &item.Key
fmt.Printf("newS[k]的地址为: %p\n", newS[k])
fmt.Printf("newS[k]的值为: %s\n", *newS[k])
}
f, _ := json.Marshal(newS)
fmt.Println(string(f))
}
func ValueAndPoint() {
fmt.Println("------------------简单变量")
var hello string = "hello"
fmt.Println("值为", hello)
fmt.Printf("字符串字节长度为%d\n", unsafe.Sizeof(hello))
fmt.Println("地址为", &hello)
fmt.Printf("地址为%p\n", &hello)
//结构体
t := Test{
Hello: "hello",
World: "world",
}
fmt.Println("------------------结构体")
fmt.Println("值为", t)
fmt.Printf("结构体字节长度为%d\n", unsafe.Sizeof(t))
fmt.Println("地址为", &t)
fmt.Printf("地址为%p\n", &t)
fmt.Printf("地址为%p\n", &t.Hello)
fmt.Printf("地址为%p\n", &t.World)
//地址存放
fmt.Println("------------------地址存放")
var save *Test = &t
fmt.Printf("值为%p\n", &*save)
fmt.Printf("指针字节长度为%d\n", unsafe.Sizeof(save))
fmt.Printf("地址为%p\n", &save)
}
func NewCase() {
fmt.Println("------------------正确")
var a, b string = "a", "b"
ai := ImageItem{
Key: a,
Name: a,
FileType: a,
}
bi := ImageItem{
Key: b,
Name: b,
FileType: b,
}
l := make(map[int]*ImageItem)
l[0] = &ai
l[1] = &bi
s := make([]*ImageItem, 2)
//指针赋值,没影响
for k, item := range l {
fmt.Println("------------------第", k+1, "次循环")
fmt.Printf("原数据的指针地址为: %p\n", l[k])
fmt.Printf("原数据的数据为: %v\n", l[k])
fmt.Println("item的地址为", &item)
fmt.Printf("item的值为: %p\n", item)
s[k] = item
fmt.Println("s[k]的地址为", &s[k])
fmt.Printf("s[k]的值为: %p\n", s[k])
}
f, _ := json.Marshal(s)
fmt.Println(string(f))
}
func NormalErrorCase() {
fmt.Println("------------------错误")
var a, b string = "a", "b"
ai := ImageItem{
Key: a,
Name: a,
FileType: a,
}
bi := ImageItem{
Key: b,
Name: b,
FileType: b,
}
l := make(map[int]ImageItem)
l[0] = ai
l[1] = bi
s := make([]*ImageItem, 2)
//指针赋值,没影响
for k, item := range l {
s[k] = &item
}
f, _ := json.Marshal(s)
fmt.Println(string(f))
}
func valueAndPointMain() {
//ErrorShow()
//ValueAndPoint()
//NewCase()
NormalErrorCase()
}