-
Notifications
You must be signed in to change notification settings - Fork 2
/
struct.go
63 lines (47 loc) · 1.08 KB
/
struct.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
package main
import "fmt"
type User struct {
name string
age int
gender string
city string
}
// 方法定义
func (u *User) describe() {
fmt.Printf("%v 今年 %v岁了\n", u.name, u.age)
}
//指针参数
func (u *User) setAge(age int) {
u.age = age
}
//值参数
func (u User) setName(name string) {
u.name = name
}
func main() {
fmt.Println("结构体开始了")
user := User{name: "董广明", age: 31, gender: "man", city: "南京"}
fmt.Println(user)
user.describe()
user.setAge(99)
fmt.Println(user.age)
user.setName("dongguangming")
fmt.Println(user.name)
user.describe()
//简写
u := User{"董广明", 31, "man", "南京"}
fmt.Println(u)
//通过指针访问
u1 := &User{"董广明", 31, "man", "南京"}
fmt.Println(u1.name)
emp := struct {
firstName, lastName string
age, salary int
}{
firstName: "董",
lastName: "广明",
age: 31,
salary: 5000,
}
fmt.Println("员工", emp)
}