Last active
March 20, 2021 09:52
-
-
Save artturijalli/29ec6364bdcc83e18e5e4629c84cba68 to your computer and use it in GitHub Desktop.
A simple example of using functions in Go.
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" | |
// FUNCTIONS | |
// specify the argument type(s) and the possible return value type | |
func sum(x int, y int) int { | |
return x + y | |
} | |
// Functions with multiple return values | |
func ageDays(age int) (int, string){ | |
if age < 0 { | |
return 0,"Age cannot be negative" | |
} else { | |
return 365 * age, "" | |
} | |
} | |
func main() { | |
// Calling the Functions | |
// SUM | |
sumResult := sum(1,2) | |
fmt.Println(sumResult) // prints 3 | |
// AGEDAYS | |
days, msg := ageDays(25) | |
if msg == "" { | |
fmt.Print("Age in days:", days) | |
} else { | |
fmt.Print(msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment