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 an error stating “missing return at the end of the function.”
Typically, when a function returns two types, the second type is an error
. We have not gone over errors yet, so we won’t be demonstrating them in these examples. It is good to know that, in Go, it is idiomatic for the second return type to be of the error
type.
Go also allows you to ignore a variable being returned. For example, say we are not interested in the int
value that is being returned from our checkNumbers
function. In Go, we can use what is called a blank identifier, which allows us 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...