For more exciting content, please pay attention to wechat public number: Back-end technology Cabin
Recently, we focused on developing a wave of Golang, so we are going to open a pit, called Golang Daily Development series, to summarize all kinds of strange problems about Golang development during this period, and we will supplement them if there are new problems later.
Without further ado, let’s jump into one of the series and see what bugs we have in using defer and how we can fix them.
I. The so-called “Pit”
func logErr(err error) {
fmt.Println(err)
}
func main() error {
var err error
defer logErr(err)
err = fmt.Errorf("error")
return err
}
Copy the code
The above code attempts to print the err variable when main exits. What do you think will come out? Nil or error? Normally we would think that err has changed from nil to non-nil before returning, so we would print error when we defer? Unfortunately, that’s not the case. The output of the code is nil
Second, the reason why
Why did it turn out differently than we expected? Since logErr is the same function, when you register defer logErr (Err), the nil value of Err is passed to logErr. When the function exits to execute the defer code, it prints the passed parameter value nil.
The reason why we felt that we didn’t meet our expectations was because we didn’t understand golang defer and how the function worked
Third, solve
3.1 pointer method
Once you understand the reason, the solution is there. LogErr is a pointer to err, so that no matter how err changes, I can get the latest value from the pointer.
func logErr(err *error) {
fmt.Println(*err)
}
func main() error {
var err error
defer logErr(&err)
err = fmt.Errorf("error")
return err
}
Copy the code
The above code output is error
3.2 closure method
We know that when we use closures, the closure refers to external variables, and with this feature, we can also do what we need
func logErr(err error) {
fmt.Println(err)
}
func main() error {
var err error
defer func() {
logErr(err)
}()
err = fmt.Errorf("error")
return err
}
Copy the code
When you register defer, the reference to Err is passed into the closure. No matter how err has changed since then, when we defer, the logErr function must get the latest err value, right
Recommended reading
-
Boltdb Study Note 3 – Transaction and concurrency control
-
Boltdb Learning Notes 2 — data structures
-
Boltdb learning notes – storage management
-
Boltdb Learning Notes ○ – Overview
For more exciting content, please scan the code to follow the wechat public number: back-end technology cabin. If you think this article is helpful to you, please share, forward, read more.
This article uses the article synchronization assistant to synchronize