Created
September 6, 2018 19:35
-
-
Save viveksyngh/659011dbd48e1f787ad394fccf05fe15 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
| ) | |
| //Person type to repersent a person | |
| type Person struct { | |
| FirstName string | |
| LastName string | |
| Age int | |
| } | |
| //Pointer to person struct is receiver for birthday function | |
| //birthday increment person's age by 1 | |
| func (p *Person) birthday() { | |
| p.Age++ | |
| } | |
| //add1 adds 1 to an integer | |
| func add1(a *int) { | |
| *a = *a + 1 | |
| } | |
| func main() { | |
| //Basic types | |
| a := 10 | |
| fmt.Printf("\"a\" value before calling \"add1\" : %d\n", a) | |
| add1(&a) | |
| fmt.Printf("\"a\" value after calling \"add1\" : %d\n", a) | |
| //User defined types | |
| person := &Person{FirstName: "John", LastName: "Doe", Age: 30} | |
| fmt.Printf("Person's age before birthday : %d\n", person.Age) | |
| person.birthday() | |
| fmt.Printf("Person's age after birthday : %d\n", person.Age) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment