|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | +) |
| 7 | + |
| 8 | +// Define person struct |
| 9 | + |
| 10 | +type Person struct { |
| 11 | + firstName, lastName, city, gender string |
| 12 | + age int |
| 13 | +} |
| 14 | + |
| 15 | +// Greeting method (value reciever) |
| 16 | +func (p Person) greet() string { |
| 17 | + return "Hell, my name is " + p.firstName + " " + p.lastName + " and I am " + strconv.Itoa(p.age) |
| 18 | +} |
| 19 | + |
| 20 | +// hasBirthday method (pointer reciever) |
| 21 | +func (p *Person) hasBirthday() { |
| 22 | + p.age++ |
| 23 | +} |
| 24 | + |
| 25 | +// getMarried (pointer reciever) |
| 26 | +func (p *Person) getMarried(spouseLastName string) { |
| 27 | + if p.gender == "Männlich" { |
| 28 | + return |
| 29 | + } else { |
| 30 | + p.lastName = spouseLastName |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func main() { |
| 35 | + // Init person using struct |
| 36 | + |
| 37 | + /* person1 := Person{firstName: "Konstantin", lastName: "Komendantov", city: "Heilbronn", gender: "Männlich", age: 21} */ |
| 38 | + person2 := Person{"Konstantin", "Komendantov", "Neckarsulm-Amorbach", "Männlich", 21} |
| 39 | + person3 := Person{"Susanne", "Schmidt", "Neckarsulm-Amorbach", "Weiblich", 21} |
| 40 | + |
| 41 | + /* fmt.Println(person2) |
| 42 | +
|
| 43 | + fmt.Println(person2.firstName) |
| 44 | + person2.age++ |
| 45 | + fmt.Println(person2) */ |
| 46 | + |
| 47 | + person2.hasBirthday() |
| 48 | + fmt.Println(person2.greet()) |
| 49 | + person3.getMarried("Frank") |
| 50 | + fmt.Println(person3.greet()) |
| 51 | + |
| 52 | +} |
0 commit comments