Go - Idiomatic way to return (potentially shadowed) variables which have been defined as part of a separate multiple variable return -
normal status
While typing a function with the named return value, you typically use nude returns (Whether or not you have a different discussion). They can look like something:
func add (x, y int) (z int) {z = x + y return} Return is the same problem as return z
though with the compressed snippet below ... ( Module, ".md") {module, err: = readConvertMDFile (fileRoot + "htdocs / md /" +)
func load module (module-name, fileRoot string) (module [] byte) {if String.HasSuffix (moduleName, ".md" module_name) If mistake! = Zero {log. Printerline (fault)} return module} and {module = [] byte {} return}} (this snippet runs fine, and this is my current solution to the problem) ... The compiler will complain that module is shaded if instead of return module bus Return . The reason for this is that the module was declared with err (second time), which has to be announced because it is still not present in this area. Possible Solutions -
As I have done and clearly name the return variable name. Although this is not a terrible solution, I feel like there should be a way to organize the code so that it can run with a nude return. That this clear return leads to 'code odor'.
-
Add a var error error in the beginning and use an assignment instead of the declaration. Maybe there is a better solution, but I would like to use the built-in assignment where possible for continuity and reducing unnecessary lines. -
Assign a temporary moduleT variable then module = module T ... it's just messy and redundant. While I can get compiled results, I am hoping someone can recommend a clear, idiomatic way of writing it.
I always use your suggested solution 2 - An additional var Add statement. func loadModule (module-name, file root string) (module [] byte) {var errors error if wire HasSuffix (moduleName, ".md") {module, err = readConvertMDFile (fileRoot + "htdocs / md /" + moduleName) If mistake! = Zero {log. Printline (mistake)} return} else {// There is no need for this module to be zero if it is not written on // If a blue piece is completely legal and lane () = 0 / Option 2 is also the most effective measure Remember that all the values on the stack are returned, hence the named return value A stack is equivalent to the allocated variable. If you do not have a naked return in Option 1 or Option 3, then there is a vested module = module or module = module T statement anyway . Unfortunately, there is something like that which bites every go programmer after some time. I like the compiler very much so that the function does not allow all to hide inside because it is the source of the actual insect.
Comments
Post a Comment