-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_struct.go
38 lines (29 loc) · 746 Bytes
/
16_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
//Structures
package main
import "fmt"
//A struct is a collection of fields.
//Each field has a name and a type.
//Create a struct with 3 fields
type Pony struct {
Name string
Color string
Age int
}
func (p Pony) Rename(newName string) {
p.Name = newName
}
func main() {
//Create a value of that type.
var twilightSparkle Pony
twilightSparkle.Name = "Twilight Sparkle"
twilightSparkle.Color = "Purple"
twilightSparkle.Age = 3
fmt.Println(twilightSparkle)
//Struct fields are accessed using a dot.
fmt.Println(twilightSparkle.Name)
twilightSparkle.Color = "White"
fmt.Println(twilightSparkle)
twilightSparkle.Rename("Spike")
fmt.Println(twilightSparkle.Name)
//Q: Why does it print the original Name? How to fix it?
}