Naked Returns
Note
Functions that have return values must have a return
statement as the last statement in the function. If you omit the return statement, the Go compiler will give you the following error: "missing return at the end of function."
Typically, when a function returns two types, the second type is of type error
. We have not gone over errors yet so in these examples, we are not demonstrating them. It is good to know that it is idiomatic in Go for the second return type to be of type error
.
Go also allows the ability to ignore a variable being returned. For example, say we are not interested in the int
value that is being returned from our fizzBuzz
function. In Go, we can use what is called a blank identifier; it provides a way to ignore values in an assignment:
_, err := file.Read(bytes)
For example, when reading a file, we might not be concerned about the number of bytes read. So, in that case, we can ignore the value being returned by using the...