# Functions and Naked Returns In Go

DevFeed: [Functions and Naked Returns In Go](<https://devfeed.tech/articles/functions-and-naked-returns-in-go-22078.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2013/10/functions-and-naked-returns-in-go.html>)

Published: 2013-10-10T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [Error Handling](<https://devfeed.tech/topics/error-handling.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [ardan-labs](<https://devfeed.tech/tags/ardan-labs.md>), [blog](<https://devfeed.tech/tags/blog.md>), [code](<https://devfeed.tech/tags/code.md>), [error-handling](<https://devfeed.tech/tags/error-handling.md>), [fmt](<https://devfeed.tech/tags/fmt.md>), [function](<https://devfeed.tech/tags/function.md>), [go](<https://devfeed.tech/tags/go.md>), [go-error-handling](<https://devfeed.tech/tags/go-error-handling.md>), [go-programming](<https://devfeed.tech/tags/go-programming.md>), [golang](<https://devfeed.tech/tags/golang.md>), [programming](<https://devfeed.tech/tags/programming.md>), [scope](<https://devfeed.tech/tags/scope.md>), [shadowing](<https://devfeed.tech/tags/shadowing.md>), [variable](<https://devfeed.tech/tags/variable.md>)

## AI overview

This Go tutorial explains returning multiple values, handling errors through returned error values, ignoring a return value with an underscore, naming return arguments, and using naked returns. It also warns about variable shadowing and explains how braces create new scopes.

## Source excerpt

In Go values that are returned from functions are passed by value. Go gives you some nice flexibility when it comes to returning values from a function. Here is a simple example of returning two values from a function: package main import ( "fmt" ) func main() { id, err := ReturnId() if err != nil { fmt.Printf("ERROR: %s", err) return } fmt.Printf("Id: %d\n", id) } func ReturnId() (int, error) { id := 10 return id, nil } The function ReturnId returns a value of type integer and of type error. This is something very common that is done in Go. Error handling is performed by returning a value of type error from your functions and the calling function evaluating that value before continuing. If you don't care about the error for some reason after a function call returns, you can do something like this: id, _ := ReturnId() This time I used an underscore to represent the return value for the second return argument, which was the error. This is really nice because I don't need to declare a variable to hold the value being passed in, I can simply ignore it. You also have the option to name your return arguments: func ReturnId() (id int, err error) { id = 10 return id, err } If you name your return arguments you are creating local variables just like with your function parameters. This time when I set the id variable, I remove the colon (:) from the short variable declaration and convert it to an assignment operation. Then in the return I specify the return variables as normal. Naming your return arguments is a nice way to document what you are returning. There is also something else that you can do with your named arguments, or not do: func ReturnId() (id int, err error) { id = 10 return } This is what is called a naked return. I have removed the arguments from the return statement. The Go compiler automatically returns the current values in the return arguments local variables. Though this is really cool you need to watch for shadowing: func ReturnId() (id int, err error)