This is a creation in Article, where the information may have evolved or changed. This will be a short post, inspired by Sean Kelly's November Twitter. > I found a reason to use the specified return value in Golang and now I feel tears. > -sean Kelly (@StabbyCutyou) November 15, 2017 the goal is to remember A case where a named return variable is necessary, so let's get to the point. Imagine you're writing code that uses potentially panic functions, and you can't change those functions for whatever reason (third-party libraries, backwards compatibility, and so on). "' Gofunc Pressbutton () {fmt. Println ("I ' m Mr meeseeks, look at me!!") Other stuff and happens, but if Jerry asks to//remove 2 strokes from his golf game...panic ("It ' s gettin ' weird!")} You still need to use that function, but if it happens and you want to capture the panic and return it as an error, you should write something like this: "' Gofunc Dostuff () error {var err error//if T Here are a panic we need to recover in a deferred funcdefer func () {if r: = Recover (); r! = Nil {err = errors. New ("The Meeseeks went crazy!")}} () Pressbutton () Return err} ' execute it on Go Playground-Https://play.golang.org/p/wzkjKGqFPL after you Go run your code however ... What is it? Your error is a ' nil ' value, even when the code has an exception. It's not what we want! # # Why is this happening? Although initially it seemed that our code returned the ' var err error ' that we created at the beginning of the function, the fact that our processThe order will never reach this line of code. This means that it never returns a specific ERR variable, and changes it within our defer function, eventually becoming meaningless. Adding a ' Println ' after calling ' Pressbutton ', but before returning, does help to illustrate this point: ' Gopressbutton ()//Nothing below here gets executed!fmt. Println ("We Got here!") return err "' Execute it on Go Playground-https://play.golang.org/p/Vk0DYs20eB## How we fix it in order to fix this problem, we can Simply use the name to return the variable. "' Gofunc Dostuff (err Error) {//If there is a panic we need to recover in a deferred funcdefer func () {If r: = Recove R (); R! = Nil {err = errors. New ("The Meeseeks went crazy!")}} () Pressbutton () return err} ' executes it on Go Playground-Https://play.golang.org/p/bqGOroPjQJ The final code looks very similar, but by naming our return variable, When we declare this function, our program will return the ' ERR ' variable immediately, even if we never touched the return statement at the end of the ' dostuff ' function. Due to this subtle difference, we can now change the ' err ' variable in our defer function and successfully capture this panic.
via:https://www.calhoun.io/using-named-return-variables-to-capture-panics-in-go/
Author: Jon Calhoun Translator: Maple24 proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
460 Reads