Created
September 6, 2018 19:32
-
-
Save viveksyngh/ed0dda46cd63847b448b84c44f9443e8 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 | |
| } | |
| //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