A function can take zero or more typed arguments. The type comes after the variable name. Functions can be defined to return any number of values that are always typed.
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(42, 13))
}
In the following example, instead of declaring the type of each parameter, we only declare one type that applies to both.
package main
import "fmt"
func add(x, y int) int {
return x + y
}
func main() {
fmt.Println(add(42, 13))
}
In the following example, the location function returns two string values.
func location(city string) (string, string) {
var region string
var continent string
switch city {
case "Los Angeles", "LA", "Santa Monica":
region, continent = "California", "North America"
case "New York", "NYC":
region, continent = "New York", "North America"
default:
region, continent = "Unknown", "Unknown"
}
return region, continent
}
func main() {
region, continent := location("Santa Monica")
fmt.Printf("Matt lives in %s, %s", region, continent)
}
Functions take parameters. In Go, functions can return multiple “result parameters”, not just a single value. They can be named and act just like variables.
If the result parameters are named, a return statement without arguments returns the current values of the results.
func location(name, city string) (region, continent string) {
switch city {
case "New York", "LA", "Chicago":
continent = "North America"
default:
continent = "Unknown"
}
return
}
func main() {
region, continent := location("Matt", "LA")
fmt.Printf("%s lives in %s", region, continent)
}
I personally recommend against using named return parameters because they often cause more confusion than they save time or help clarify your code.
http://www.golangbootcamp.com/book/basics