-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.结构体.go
55 lines (49 loc) · 960 Bytes
/
11.结构体.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
package main
import "fmt"
func main() {
//go通过struct自定义类型实现面向对象
type Person struct {
name string
age int
gender string
hobby []string
}
//实例化1
person1 := Person{
name: "Tom",
age: 45,
hobby: []string{"足球", "篮球", "唱歌"},
}
fmt.Println(person1, person1.age)
//实例化2
var person2 = new(Person)
person2.age = 25
person2.name = "Well"
fmt.Println(person2, *person2)
//实例化3
var person3 = &Person{
age: 23,
}
person3.name = "Bill"
fmt.Println(person3, *person3)
//实例化4 但是必须全部初始化
person4 := Person{
"Joe", 24, "", nil,
}
fmt.Println(person4)
//匿名结构体 通常用来只使用一次的场合
var user struct {
name string
age int
}
fmt.Println(user)
//指针
p5 := &person4
p5.name = "Jhon" //相当于(*p5)
fmt.Println(p5)
//读取控制台输入
var str string
var i int
fmt.Scan(&str, &i)
fmt.Println(str, i)
}