Last active
June 28, 2016 23:08
-
-
Save dflima/a933cd61546cfb0e90798f43d3cef930 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" | |
| type person struct { | |
| age int | |
| } | |
| func (p person) NewPerson(initialAge int) person { | |
| //Add some more code to run some checks on initialAge | |
| if initialAge <= 0 { | |
| fmt.Println("Age is not valid, setting age to 0.") | |
| p.age = 0 | |
| } | |
| return p | |
| } | |
| func (p person) amIOld() { | |
| //Do some computation in here and print out the correct statement to the console | |
| switch { | |
| case p.age < 13: | |
| fmt.Println("You are young.") | |
| break | |
| case p.age >= 13 && p.age < 18: | |
| fmt.Println("You are a teenager.") | |
| break | |
| case p.age >= 18: | |
| fmt.Println("You are old.") | |
| break | |
| } | |
| } | |
| func (p person) yearPasses() person { | |
| //Increment the age of the person in here | |
| p.age += 1 | |
| return p | |
| } | |
| func main() { | |
| var T,age int | |
| fmt.Scan(&T) | |
| for i := 0; i < T; i++ { | |
| fmt.Scan(&age) | |
| p := person{age: age} | |
| p = p.NewPerson(age) | |
| p.amIOld() | |
| for j := 0; j < 3; j++ { | |
| p = p.yearPasses() | |
| } | |
| p.amIOld() | |
| fmt.Println() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment