Created
January 7, 2023 03:16
-
-
Save xvbnm48/a660661fff5776c65a6a77570770d346 to your computer and use it in GitHub Desktop.
golang method, struct, and pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
type Person struct { | |
name string | |
age int | |
} | |
func (p *Person) Birthday() { | |
p.age++ | |
} | |
func (p Person) Greet() string { | |
return "Hello, my name is " + p.name + " and I am " + strconv.Itoa(p.age) + " years old." | |
} | |
func main() { | |
p := Person{"Sakura Miyawaki", 20} | |
p.Birthday() | |
fmt.Println(p.Greet()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment