Essential Go Functions  Suggest an edit

Named return values

You can name returned values in which case they act as implicit local variable.

An empty return statement can then be used to return their current values. This is known as “naked” return.

func inverse(v float32) (reciprocal float32) {
	if v == 0 {
	reciprocal = 1 / v

// function can return multiple values
func split(sum int) (x, y int) {
	x = sum * 4 / 9
	y = sum - x

func main() {
	fmt.Printf("inverse(5)=%.2f\n", inverse(5))

Two important things must be noted:

Using named return values is considered a bad practice that hinders readability, especially in longer function. Explicit is better than implicit.

  ↑ ↓ to navigate     ↵ to select     Esc to close