-
Notifications
You must be signed in to change notification settings - Fork 0
/
structures.go
53 lines (44 loc) · 1.29 KB
/
structures.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
package main
import (
"fmt"
"strconv"
)
// define struct, for instance Person
type Person struct {
firstname string
lastname string
city string
gender string
age int
}
//methods
// there are two types of methods
// 1.value recievers---> they dont change anything
// 2. pointer recievers--> changing something
// example greeting method ==> Value reciever
func (p Person) greeting() string {
return "hi my name is" + " " + p.firstname + " " + p.lastname + " " + "and am from " + p.city + " " + "aged" + " " + strconv.Itoa(p.age)
}
// this is a pointer reciever that will be used to change the struct above (using asterisk is good for this kind of method)
func (p *Person) aged() {
p.age++
}
func main() {
// intialize your struct using property name
person1 := Person{firstname: "james", lastname: "Walker", city: "Nairobi", gender: "Male", age: 34}
fmt.Println(person1)
//alternative
person2 := Person{"Dennis", "Smith", "Nakuru", " male", 23}
fmt.Println(person2)
// calling the pointer reciever in order to increase their age a couple of times before calling it
person2.aged()
person2.aged()
person2.aged()
person2.aged()
person2.aged()
person2.aged()
person2.aged()
person2.aged()
// calling the method greeting above(value reciever)
fmt.Println(person2.greeting())
}