-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
47 lines (38 loc) · 1.02 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
package main
import "fmt"
func main() {
/*
Go is not an Object Oriented language,
instead of creating classes you can create structures.
Fields can have any kind of data, even other structs.
We can also associate method to structs (later on)
*/
type person struct {
name string
lastname string
age int
}
// Creating values from `person` struct
jimmy := person{
name: "Jimmy",
lastname: "Smith",
age: 24, // Not the comma after the last field
}
fmt.Println(jimmy)
fmt.Printf("%v %v is %v years old.\n", jimmy.name, jimmy.lastname, jimmy.age)
// No need to instantiate a struct fields right away, they default to zero values
johnny := person{}
// Accessing and setting single field
johnny.name = "Johnny"
fmt.Println(johnny)
/*
Structs are values, you can clone
a struct by simply assigning it to
a new variable
*/
anotherJimmy := jimmy
// Updating the new var
anotherJimmy.lastname = "Adams"
fmt.Println("Original Jimmy:", jimmy)
fmt.Println("New Jimmy:", anotherJimmy)
}