Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: Declare both a new variable and overwrite a variable from a higher scope, how?

I cannot find the answer anywhere, so this is the situation:

// err has not yet been declared here
globalVar := "string"
if globalVar == "string" {
    globalVar, err := doSomethingWithString()
    if err != nil {
        // error handling
    }
}

That second globalVar declaration gives an error both then := and when '=' is used:

  • With := it says globalVar declared and not used because now globalVar is a new variable in the inner scope.
  • With = it says undefined err because it has not yet been declared.

So basically, there should be a way to define the difference between = and := for each variable on the left side of the declaration.

I see two possible solutions, which are both equally ugly in my opinion:

// err has not yet been declared here
globalVar := "string"
if globalVar == "string" {
    globalVar2, err := doSomethingWithString()
    if err != nil {
        // error handling
    }
    globalVar = globalVar2
}

or

globalVar := "string"
var err error
if globalVar == "string" {
    globalVar, err = doSomethingWithString()
    if err != nil {
        // error handling
    }
}

Do I have to use one of these work-arounds or is there a correct way of achieving what I need?

The second solution seems the least ugly, but if there are many variables you only need in the if-scope, these variables will not be removed after the scope and persist the whole outer scope. So the fist solution seems the best in my opinion.

But I'd like to hear how others resolve this situation...

like image 229
Steven Roose Avatar asked Jun 14 '13 13:06

Steven Roose


People also ask

What are two ways to create a new variable in Golang?

In Go language variables are created in two different ways: Using var keyword: In Go language, variables are created using var keyword of a particular type, connected with name and provide its initial value. Important Points: In the above syntax, either type or = expression can be omitted, but not both.

Can you declare a variable more than once?

Declaring multiple variables in a single declaration could cause confusion about the types of variables and their initial values.

How do you set a global variable in Go?

Local variables exist within functions. Here we use var g = "global" to create a global variable outside of the function. Then we define the function printLocal() . Inside of the function a local variable called l is assigned and then printed out.


1 Answers

Just don't use := in this case and predeclare both globalVar and err.

var (
    globalVar string
    err error
)

globalVar = "string"
if globalVar == "string" {
    globalVar, err = doSomethingWithString()
    if err != nil {
        // error handling
    }
}

or alternatively if you want to limit the scope of err:

globalVar := "string"
if globalVar == "string" {
    var err error
    globalVar, err = doSomethingWithString()
    if err != nil {
        // error handling
    }
}

http://play.golang.org/p/73bF_tHYsC for an example similar to the previous answer.

:= is meant as a convenience when you want to declare and assign in one statement. If it's getting in the way just don't use it.

like image 152
Kamil Kisiel Avatar answered Oct 23 '22 02:10

Kamil Kisiel