Skip to content

Commit 5cdeea7

Browse files
committed
结构体实例更新
1 parent 67e776d commit 5cdeea7

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,10 +1058,34 @@ int val = a[2][3]
10581058
<summary>结构类型 struct</summary>
10591059
10601060
```go
1061-
type identifier struct {
1062-
field1 type1
1063-
field2 type2
1064-
...
1061+
package main
1062+
import "fmt"
1063+
type Vertex struct {
1064+
X int
1065+
Y int
1066+
}
1067+
func main() {
1068+
fmt.Println(Vertex{1, 2})
1069+
1070+
// 结构体字段使用点号来访问。
1071+
v := Vertex{1, 2}
1072+
v.X = 4
1073+
fmt.Println(v.X)
1074+
1075+
// 结构体字段可以通过结构体指针来访问。
1076+
e := Vertex{1, 2}
1077+
p := &e
1078+
p.X = 1e9
1079+
fmt.Println(e)
1080+
1081+
1082+
var (
1083+
v1 = Vertex{1, 2} // 类型为 Vertex
1084+
v2 = Vertex{X: 1} // Y:0 被省略
1085+
v3 = Vertex{} // X:0 和 Y:0
1086+
p = &Vertex{1, 2} // 类型为 *Vertex , 特殊的前缀 & 返回一个指向结构体的指针
1087+
)
1088+
fmt.Println(v1, p, v2, v3)
10651089
}
10661090
```
10671091

0 commit comments

Comments
 (0)