Empty return in func with return value in golang

I was reading some code written in Golang

on Github and found a very interesting piece of code. I have simplified it to make it clear.

func  Insert(docs ...interface{}) (err error) {
    for i := 0; i < 3; i++ {
        err = fmt.Errorf("")
        if err.Error()!="EOF" {
            return
        }
    }
    return 
}

      

I am very puzzled by the empty return here ... How does it work? Does it return zero as an error or breaks for the loop? I understand this question looks stupid, but I can't find any information on this in the go docs ... Also, I don't understand how we can return an error, which I understood is being declared somehow instead. Does (error) mean that we already have an error variable available in ours func

that is used as the default return value if nothing is specified? Why then do we implicitly return an error at the end func

?

I will be very grateful for an explanation.

+10


source to share


3 answers


The function uses the return value "named".

From specification from return operators:

An expression list can be empty if the function's result type specifies names for its result parameters. Result parameters act like regular local variables, and the function can assign values ​​to them as needed. The return statement returns the values ​​of these variables.

Regardless of how they are declared, all result values ​​are initialized to null for their type when entering the function. The "return" statement that sets the results sets the result of the parameters before executing any deferred functions.

Using named returns allows you to save some code when manually allocating local variables and sometimes clean up messy if / else statements or long lists of return values.

func a()(x []string, err error){
    return
}

      

is actually just shorthand for

func a() ([]string,error){
  var x []string
  var err error
  return x,err
}

      

This is a bit shorter, and I agree that it might be less obvious.



Return names are sometimes required, as this allows you to do things like access them inside a deferred function, but bare return is just syntactic sugar as far as I can tell and is never strictly required.

One place I see is usually in error return cases in functions that have many return values.

if(err != nil){
   return
}
return a,b,c,nil

      

easier than

if(err != nil){
   return nil,nil,nil,err
}
return a,b,c,nil

      

when you need to write it multiple times. And you don't need to change these returns if you change the signature to have additional "real" return values.

In most of the places I use them in the codebase I just searched for, they seem to hide other smells like overly complex multi-purpose functions, too deep if / else is nested, and the like.

+15


source


If you have a named return value ( err

here):

func  Insert(docs ...interface{}) (err error) {

      

This creates a local variable of the type with that name, and if you just call return

with no parameters, it returns the local variable. So in this function

return

      



Same as means

return err

      

More details in the tour and in the spec .

+4


source


Go's return values ​​can be named. If so, they are treated as variables defined at the top of the function.

package main

import "fmt"

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
   return
}

func main() {
    fmt.Println(split(17))
}

      

https://tour.golang.org/basics/7

+3


source







All Articles