Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable scope inside if statements

Tags:

go

I've just started learning Go today and have got stuck on variable scopes.

I've ultimately confused about how to get around the fact that I can't create a variable inside an if statement and the use it afterward.

This is my code. The problem is that new1 can't be created before the if statement because its size is dependent upon the result of the if statement, and by creating it inside the if statement I can't use it after the if statement ends.

if len(array1)>len(array2) {
    new1 := make([]string,0,len(array1))
    mc := Array2Map_string(array1)
    for _,tok :=range array2 {
        _, ok := mc[tok]
        if ok {
            new1[len(new1)]=tok
            }
        }
    } else {
    new1 := make([]string,0,len(array2))
    mc := Array2Map_string(array2)
    for _,tok :=range array1 {
        _, ok := mc[tok]
        if ok {
            new1[len(new1)]=tok
            }
        }
    }
new2 := make([]string,0,len(new1))
copy(new2, new1)

The only thing I can think of is to do something like

var pointlessvariable uint
if len(array1)>len(array2) {
pointlessvariable=len(array1)
} else {
pointlessvariable=len(array2)
}
var new1 = make([]string,0,pointlessvariable)
if len(array1)>len(array2) {
...

To be quite honest if that is truly the solution then I don't think I want to use Golang after all.

So what is the best way of solving this?

like image 860
Alasdair Avatar asked Jun 29 '14 10:06

Alasdair


People also ask

Can you define a variable inside an if statement?

If you're new to the syntax that's used in the code sample, if (int i = 5) { is a perfectly valid way of declaring and defining a variable, then using it inside the given if statement. It allows us to write terser, clearer code, while also avoiding limiting the scope of a variable.

DO if statements change scope?

Does an if statement create a new scope? The body of an if statement does not create a new scope. Any variables assigned within the body of an if statement will modify the scope that the if statement itself is in.

Are variables inside if statements global?

Re: Using global variables in functions with if statements There are global and local variables. All variables declared in the first (global) namespace are global. All other variables (declared in a function) are local in the function.

Can you assign variables in an if statement Python?

Python variables are scoped to the innermost function, class, or module in which they're assigned. Control blocks like if and while blocks don't count, so a variable assigned inside an if is still scoped to a function, class, or module.


2 Answers

You can declare new1 before the if block and use make inside:

var new1 []string

if len(array1)>len(array2) {
    new1 = make([]string, 0, len(array1))
    // instructions ...
} else {
    new1 = make([]string, 0, len(array2))
    // other instructions ...
}

new2 := make([]string, 0, len(new1))
copy(new2, new1)
like image 172
julienc Avatar answered Sep 17 '22 20:09

julienc


Slightly related to the question of variable scope, new gophers (go programmers) might find it interesting that you can also arbitrarily force variable scope with a pair of curly brackets { } anywhere in the code. You don't need a keyword for this.

Example:

// outside of scope
var color = "blue"

{
    // inside a new scope
    var color = "red"
    fmt.Println(color) // prints red
}

// outside of scope again
fmt.Println(color) // prints blue again
like image 33
kintsukuroi Avatar answered Sep 20 '22 20:09

kintsukuroi