-
Notifications
You must be signed in to change notification settings - Fork 1
/
PublicPrivate.go
76 lines (61 loc) · 1.47 KB
/
PublicPrivate.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
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"./fruits"
"encoding/json"
"fmt"
)
func main() {
fmt.Println("Using fruits interface to print the different types of fruit and see what fields get left out.")
// pear := Fruits.pear {} <-- 404 - Pear not found
// Because pear is in lowercase and therefor not exported
banana := fruits.Banana{
Colour: "Yellow",
Weight: 4,
}
fruits.PrintFruit(banana)
apple := fruits.Apple{
Colour: "Green",
}
fruits.PrintFruit(apple)
orange := fruits.Orange{}
fruits.PrintFruit(orange)
passionFruit := passionFruit{
colour: "Purple",
weight: 4,
}
fruits.PrintFruit(passionFruit)
fmt.Println()
fmt.Println("Using built in Json package to get json strings from objects, so we can see fields getting left out.")
bJson, e := json.Marshal(banana)
if e != nil {
fmt.Println(e)
}
fmt.Println("Banana:", string(bJson))
aJson, e := json.Marshal(apple)
if e != nil {
fmt.Println(e)
}
fmt.Println("Apple", string(aJson))
oJson, e := json.Marshal(orange)
if e != nil {
fmt.Println(e)
}
fmt.Println("Orange", string(oJson))
pfJson, e := json.Marshal(passionFruit)
if e != nil {
fmt.Println(e)
}
fmt.Println("Passion fruit", string(pfJson))
}
type passionFruit struct {
colour string
weight int
}
// Prints out the weight of the Passion fruit
func (pf passionFruit) PrintWeight() {
fmt.Println("Weight:", pf.weight)
}
// Prints out the Colour of the Passion fruit
func (pf passionFruit) PrintColour() {
fmt.Println("Colour:", pf.colour)
}