This Go program uses the return statement in 5 separate funcs. The funcs return zero, 1, or more than 1 value in different ways.
package main
import
"fmt"
// Part 1: can return a single value from a function.
func Example1() int {
return 10
}
// Part 2: can return no value (void method).
func Example2() {
fmt.Println(
"Inside Example2")
return
}
// Part 3: can return multiple values that are not named.
func Example3() (string, int) {
return
"bird", 10
}
// Part 4: can return multiple named values.
func Example4() (name string, count int) {
name =
"bird"
count = 10
return
}
// Part 5: only the named return values need to be assigned.
func Example5() (name string, _ int) {
name =
"bird"
return
}
func main() {
// Invoke the example methods.
result := Example1()
fmt.Println(
"Example1", result)
Example2()
name, count := Example3()
fmt.Println(
"Example3", name, count)
name, count = Example4()
fmt.Println(
"Example4", name, count)
name, count = Example5()
fmt.Println(
"Example5", name, count)
}
Example1 10
Inside Example2
Example3 bird 10
Example4 bird 10
Example5 bird 0