1

I have the code below:

if err == nil {
    body, err := ioutil.ReadAll(response.Body)
    if err == nil {
        dataMap := &models.UserResponse{}
        json.Unmarshal(body, &dataMap)
        if dataMap.User == (models.UserId{}) {
            err = fmt.Errorf("unauthorized")
            fmt.Println(err) // when unathorized, prints unauthorized
        }
    }
}

fmt.Println(err) // always prints nil

The Println inside if dataMap.User ... prints "unauthorized", whereas the last Println always prints nil.

I have no idea why it happens, err is declared via var err error at the beginning of this function.

icza
  • 389,944
  • 63
  • 907
  • 827

2 Answers2

6

The cause is detailed in Spec: Short variable declaration:

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

When using short variable declaration with multiple variables where one already exists, assignment will only happen to the existing variable if it was declared in the same block. Since in your case err variable existed before the if block, a new err variable will be created inside the if block, which has nothing to do with the "outsider" err variable (other than sharing its name). The outer err will be shadowed in the if block after the short variable declaration.

So what happens is that inside the if, you create a new err variable, and you assign a value to that, and you print that.

After the if statement, you will print the outer err variable whose value was not changed inside the if block, so it remains nil.

See this example:

var err error
fmt.Println("outside:", err) // nil

{
    // A new err variable, shadows the outer:
    i, err := strconv.Atoi("a")
    fmt.Println("inside:", i, err) // 0 strconv.Aoti: parsing "a": invalid syntax
}

fmt.Println("outside:", err) // nil, because this was never changed

// Now this will change the "outer" err:
j, err := strconv.Atoi("a")
fmt.Println("outside:", j, err) // 0 strconv.Aoti: parsing "a": invalid syntax

Output (try it on the Go Playground):

outside: <nil>
inside: 0 strconv.Atoi: parsing "a": invalid syntax
outside: <nil>
outside: 0 strconv.Atoi: parsing "a": invalid syntax

If you want to use (assign to) the "outer" variable when also creating new variable(s), you can't use short variable declaration in a "nested" block but only simple assignment, in which case you also have to declare the other variables a priori, like in this example:

if err == nil {
    var body []byte
    body, err = ioutil.ReadAll(response.Body)
    // ... rest...
}

See related questions:

Why it is possible to redefine err in multiple return statement in Go

Why there are two ways of declaring variables in Go, what's the difference and which to use?

icza
  • 389,944
  • 63
  • 907
  • 827
-1

You have created err inside block but you are printing same outside of block. Declare error outside of block and initialize it anywhere inside that will fetch the value when printing.

var err error
fmt.Println(err) // always prints nil
Himanshu
  • 12,071
  • 7
  • 46
  • 61
  • This might give a compilation error `undefined body`. – James K Apr 09 '18 at 11:01
  • Oh I mistakenly removed the assignment operator for body. Main purpose was to declare err outside of block – Himanshu Apr 09 '18 at 11:03
  • 1
    But this now has the same shadowing problem from the original question... I'd declare body (inside or outside the block as required) and use assignment (`=`) to redefine the value of err declared outside the block. – James K Apr 09 '18 at 11:16
  • using assignment operator you are initializing the value of err and if you wants to print error inside the body you have to print it inside the block as it will have different value outside the body. – Himanshu Apr 09 '18 at 11:31