Reputation: 6953
I'm following golang wiki instruction and find this confusing:
func loadPage(title string) (*Page, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
If err not equal nil, why return nil?
Isn't it supposed to be:
if err != nil {
return err
}
like it is described in golang errors and values?
Upvotes: 3
Views: 6929
Reputation: 12675
In loadpage function you are returning two values, one is a pointer to Page and other is an error.
func loadPage(title string) (*Page, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
That's the reason if there is an error in the code and it is not equal to nil. Then return the error. But since you need to return Page also it should be nil since which denotes zero value for pointer. This shows that the value for Page is empty.
return &Page{Title: title, Body: body}, nil
But if there is no error then again you are returning two values one is the address of Page struct with values assigned to the Page fields but nil for error.
If a value of a type does not contains any value. It should be initialize with zero value which is
false for booleans, 0 for numeric types, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.
The return value or values may be explicitly listed in the "return" statement. Each expression must be single-valued and assignable to the corresponding element of the function's result type.
func complexF1() (re float64, im float64) {
return -7.0, -4.0
}
The expression list in the "return" statement may be a single call to a multi-valued function. The effect is as if each value returned from that function were assigned to a temporary variable with the type of the respective value, followed by a "return" statement listing these variables, at which point the rules of the previous case apply.
func complexF2() (re float64, im float64) {
return complexF1()
}
Note:
Regardless of how they are declared, all the result values are initialized to the zero values for their type upon entry to the function. A "return" statement that specifies results sets the result parameters before any deferred functions are executed.
Have a look on Golang spec for Return statements
Upvotes: 4
Reputation: 166569
The Go Programming Language Specification
When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value is set to the zero value for its type: false for booleans, 0 for numeric types, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps. This initialization is done recursively, so for instance each element of an array of structs will have its fields zeroed if no value is specified.
Because of the error, there is no valid value to return so we return the zero value, which happens to be nil
(for a pointer value) in this case.
Upvotes: 2
Reputation: 3036
The function returns 2 things: A Page (pointer) and an error. If an error occurs, there is no page to be returned, hence the first value returned is nil
. The second value being returned is the error, err
, as it should.
Upvotes: 6