-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10-struct.go
More file actions
66 lines (46 loc) · 1.34 KB
/
10-struct.go
File metadata and controls
66 lines (46 loc) · 1.34 KB
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
/*
* @Author: Barnabas Makonda
* @Date: 2019-01-15 22:46:56
* @Last Modified by: Barnabas Makonda
* @Last Modified time: 2019-01-16 11:06:25
*/
package main
import "fmt"
/*
* A struct is aggregate data type. it groups together other objects of abitrary type.
*
*/
// Person represent person
type Person struct {
name string
address string
phone string
}
// Employee represent employee
type Employee struct {
Person
position string
}
// Talk is the method to print out the name or the person.
func (p *Person) Talk() {
fmt.Println("Hi my name is ", p.name)
}
func main() {
// Strut Person which agregate types of name, address and phone as fields of type Person
var tzPresident Person
// Accessing the struct fields, we use dot notation
tzPresident.name = "John Pombe Magufuli"
tzPresident.address = "Magogoni street"
tzPresident.phone = "(222) 0 1234445"
x := tzPresident.name
fmt.Println(x)
// Initializing struct we can use new() which will initialize all fields to zero data of that field type. if int, then 0 if string then ""
p2 := new(Person)
fmt.Println(p2)
// OR initialize using struct literal
p1 := Person{name: "joe", address: "Mazinge st.", phone: "0277337733"}
fmt.Println(p1)
// Embedded types
mlinzi := Employee{Person{name: "Juma", address: "Mazinge st.", phone: "0277337733"}, "Mlinzi"}
mlinzi.Talk()
}