-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathStruct.go
39 lines (31 loc) · 768 Bytes
/
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
package main
import "fmt"
// Declaring a Struct
type Animal struct {
name string
weight int
}
func main() {
// Creating an instance of a struct
var dog Animal
dog.name = "Dog"
dog.weight = 40
fmt.Println(dog)
// Creating an instance using struct literate
var cat = Animal{name: "Cat", weight: 5}
fmt.Println(cat)
// Creating an instance using the new keyword
var bird = new(Animal)
bird.name = "Bird"
bird.weight = 1
fmt.Println(bird)
// Creating an instance using the pointer address operator
var monkey = &Animal{name: "Monkey", weight: 10}
fmt.Println(monkey)
// Comparing struct instances
fmt.Println(bird == monkey)
// Copying struct type using pointer reference
monkey2 := monkey
monkey2.name = "Monkey2"
fmt.Println(monkey2)
}