Member-only story
Go’s new proposal: Should return values be explicitly used or ignored?
Previously, when writing Go code, the IDE often prompted. In addition, I have a friend whose CodeReview team also encounters the issue of whether to handle the return value of some methods. At first, we would discuss it, but over time, we basically became numb.
During the holiday, while flipping through study materials, I came across the Go community’s related issue #20803 [1]. Previously, a boss had raised similar doubts, and the Go team also responded.
The official has given a preliminary conclusion, which I will share with everyone today.
Quick background
Now when we write Go programs, if a function or method returns both a return value and an error parameter, the user (programmer) must take some action.
The most common scenario for returning the error parameter. The following code:
v, err := computeTheThing()
Or explicitly ignore him. The following code:
v, _ := computeTheThing()
I believe everyone has done this before. (Yes, I often see it in code…)
However, in many scenarios with unique return values (such as: io. Closer. Close
, proto. Unmarshal
), after getting used to it, some will ignore it.
The most common code:
_ = json.Unmarshal(jsonBlob, &eddycjy)