Why is variable declaration not allowed here?

soldier.moth

Why does testC() fail to compile in the following go code? I would expect that the behavior would match that of testB() with the exception that err's scope would be limited to the if block.

The error message provided by the compiler is resp declared and not used

package main

import "fmt"

func main() {
    testA()
    testB()
    testC()
    testD()
}

// valid
func testA() {
    resp, err := generateInt()
    fmt.Println(fmt.Sprintf("Resp=%v Error=%v", resp, err))
}

// valid
func testB() {
    var resp *int
    resp, err := generateInt()
    fmt.Println(fmt.Sprintf("Resp=%v Error=%v", resp, err))
}

// does not compile
func testC() {
    var resp *int
    if resp, err := generateInt(); err != nil {
        fmt.Println("error=%v", err)
        return
    }
    fmt.Println("Resp=%d", *resp)
}

// valid
func testD() {
    var resp *int
    var err error
    if resp, err = generateInt(); err != nil {
        fmt.Println("error=%v", err)
        return
    }
    fmt.Println("Resp=%d", *resp)
}

func generateInt() (*int, error) {
    result := 5
    return &result, nil
}
JimB

In this example:

var resp *int
if resp, err := generateInt(); err != nil {

The short variable declaration is redeclaring the resp variable. Because an if's statement is scoped to the inside the if block, it only shadows the first resp variable within that block, leaving the first unused.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

variable declaration not allowed here

From Dev

variable declaration not allowed here

From Dev

Why is ;; allowed after a local variable declaration, but not after a field declaration?

From Dev

Variable declaration not allowed in Java

From Dev

Java variable declaration not allowed

From Dev

Why it is allowed to initialize static variable with non const here?

From Dev

Why isn't short variable declaration allowed at package level in Go?

From Dev

"Declaration Not allowed here" and "Constant Expression" Error in C

From Dev

Why is lambda function not allowed here?

From Dev

Why is a forward declaration in a function declaration allowed?

From Dev

Declaration and declaration with definition. Why is this not allowed?

From Dev

java.util.Scanner Object Declaration not Allowed Here

From Dev

Variable declaration not allowed. But there is no scope overlap

From Dev

Why append a vector to another vector is not allowed here?

From Dev

Why is a function declaration not a statement while a variable declaration is

From Dev

Javascript variable declaration, why is this legal?

From Dev

Why Global variable redefinition is not allowed?

From Dev

Why is a class/struct declaration with different number of template parameters not allowed?

From Dev

Why is a class/struct declaration with different number of template parameters not allowed?

From Dev

Why does this locals variable declaration not work?

From Dev

Nameless variable declaration - why does it work?

From Dev

Why does this locals variable declaration not work?

From Dev

Why declaration of variable in closure environment did not work?

From Java

Why type alias is allowed as name of variable?

From Dev

Why is jumping across variable definitions (with goto) allowed?

From Java

Why is a JavaScript reserved keyword allowed as a variable name?

From Dev

Why are multi-variable return statements allowed?

From Dev

Why are special characters not allowed in variable names?

From Dev

Why is a JavaScript reserved keyword allowed as a variable name?

Related Related

  1. 1

    variable declaration not allowed here

  2. 2

    variable declaration not allowed here

  3. 3

    Why is ;; allowed after a local variable declaration, but not after a field declaration?

  4. 4

    Variable declaration not allowed in Java

  5. 5

    Java variable declaration not allowed

  6. 6

    Why it is allowed to initialize static variable with non const here?

  7. 7

    Why isn't short variable declaration allowed at package level in Go?

  8. 8

    "Declaration Not allowed here" and "Constant Expression" Error in C

  9. 9

    Why is lambda function not allowed here?

  10. 10

    Why is a forward declaration in a function declaration allowed?

  11. 11

    Declaration and declaration with definition. Why is this not allowed?

  12. 12

    java.util.Scanner Object Declaration not Allowed Here

  13. 13

    Variable declaration not allowed. But there is no scope overlap

  14. 14

    Why append a vector to another vector is not allowed here?

  15. 15

    Why is a function declaration not a statement while a variable declaration is

  16. 16

    Javascript variable declaration, why is this legal?

  17. 17

    Why Global variable redefinition is not allowed?

  18. 18

    Why is a class/struct declaration with different number of template parameters not allowed?

  19. 19

    Why is a class/struct declaration with different number of template parameters not allowed?

  20. 20

    Why does this locals variable declaration not work?

  21. 21

    Nameless variable declaration - why does it work?

  22. 22

    Why does this locals variable declaration not work?

  23. 23

    Why declaration of variable in closure environment did not work?

  24. 24

    Why type alias is allowed as name of variable?

  25. 25

    Why is jumping across variable definitions (with goto) allowed?

  26. 26

    Why is a JavaScript reserved keyword allowed as a variable name?

  27. 27

    Why are multi-variable return statements allowed?

  28. 28

    Why are special characters not allowed in variable names?

  29. 29

    Why is a JavaScript reserved keyword allowed as a variable name?

HotTag

Archive