Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active March 31, 2021 11:20
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 Integralist/47e0f8f848ea67f93fc29b754f02cca7 to your computer and use it in GitHub Desktop.
Save Integralist/47e0f8f848ea67f93fc29b754f02cca7 to your computer and use it in GitHub Desktop.
[Golang Named Return Zero Value] #go #golang #zero #named #return

Go's return values may be named. If so, they are treated as variables defined at the top of the function.

These names should be used to document the meaning of the return values.

A return statement without arguments returns the named return values. This is known as a "naked" return.

Naked return statements should be used only in short functions. They can harm readability in longer functions.

package main

import "fmt"

func do() (b bool) {
    // var b bool << imagine the compiler added this
	return
}

func main() {
	fmt.Println(do()) // false
}

Here's a real example of using the 'zero' value properties of named returns.

In this example we have a GetString which will return a default value if a key isn't found. It does this by internally calling GetStringDefault and passing it the named return variable val that GetString has defined.

package main

import (
	"fmt"
)

func GetString(key string) (val string) {
	// 'named' return value are initialized to their zero value
	// meaning we can proxy the zero value through to a nested function
	//
	return GetStringDefault(key, val)
}

func GetStringDefault(key string, defaultValue string) string {
	m := map[string]string{
		"foo": "bar",
		"abc": "xyz",
	}

	v, ok := m[key]
	if !ok {
		return defaultValue
	}
	return v
}

func main() {
	defaultValue := "no key found"
	
	// if you care about getting a default value back
	// then use the GetStringDefault function
	//
	fmt.Println(GetStringDefault("foo", defaultValue)) // bar
	fmt.Println(GetStringDefault("abc", defaultValue)) // xyz
	fmt.Println(GetStringDefault("xxx", defaultValue)) // no key found

	// if you don't care about getting a default value back
	// then use the GetString function, and you'll now get back a zero value
	//
	fmt.Println(GetString("foo")) // "bar"
	fmt.Println(GetString("abc")) // "xyz"
	fmt.Println(GetString("xxx")) // "" << zero value for type string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment