Skip to content

Instantly share code, notes, and snippets.

@artturijalli
Last active March 20, 2021 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artturijalli/29ec6364bdcc83e18e5e4629c84cba68 to your computer and use it in GitHub Desktop.
Save artturijalli/29ec6364bdcc83e18e5e4629c84cba68 to your computer and use it in GitHub Desktop.
A simple example of using functions in Go.
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