Mixed gear and Golang declaration

I started working with a few weeks, and (once again) I came across something that seemed strange to me:

// Not working
a := 1
{
    a, b := 2, 3
}

// Works
a := 1
a, b := 2, 3

playground

I want to assign two variables at the same time. One of them has already been declared to the highest degree, the other is not.

This does not work: the compiler is trying to override the old variable. However, it works fine if this variable is declared in the same scope.

Why is this?

+4
source share
3 answers

, , shadow shadowing. := , , if for, , :

n := "Example"
//Prints the string variable `n` to standard output and
// returns the number of bytes written in int variable `n` and
// an error indicator in error variable `err`.
if n, err := fmt.Println(n); err != nil {
    panic(err)
} else {
    fmt.Println(n, "bytes written")
}

//Prints the string variable `n` to standard output.
fmt.Printf("n = %q\n", n)

:

Example
8 bytes written
n = "Example"

:

  • , , , =
  • , :=, , ; ,

, - :

if _, err := fmt.Println(n); err != nil {
    panic(err)
} else {
    fmt.Println(n, "bytes written")
}

//undefined: err
if _, err = fmt.Println(n); err != nil {
    //undefined: err
    panic(err)
}

:

  • , , , =
  • := if, ​​ ; = ,
  • = :=,

, , , , .

Go.

+4

golang :

, , .

, , a , - ': =' .

, , :

var a, b int
{
    b, a = 2, 3
    fmt.Println(b)
}
fmt.Println(a)
+4

2 :
:
=
: = vars ( var) ( ), :

package main

import (
    "fmt"
)

func main() {
    var u1 uint32      //declare a variable and init with 0
    u1 = 32            //assign its value
    var u2 uint32 = 32 //declare a variable and assign its value at once
    //declare a new variable with defining data type:
    u3 := uint32(32)        //inside the function block this is equal to: var u3 uint32 = 32
    fmt.Println(u1, u2, u3) //32 32 32
    //u3 := 20//err: no new variables on left side of :=
    u3 = 20
    fmt.Println(u1, u2, u3)       //32 32 20
    u3, str4 := 100, "str"        // at least one new var
    fmt.Println(u1, u2, u3, str4) //32 32 100 str
}

second part:
The identifier declared in the block can be updated in the inner block.
There are 4 different working samples for the scope and shadowing variables:

an easy way to limit the scope of variables:

package main
import "fmt"
func main() {
    i := 1
    j := 2
    //new scope :
    {
        i := "hi" //new local var
        j++
        fmt.Println(i, j) //hi 3
    }
    fmt.Println(i, j) //1 3
}

scope of variable restriction using function calls:

package main
import "fmt"
func fun(i int, j *int) {
    i++                //+nice: use as local var without side effect
    *j++               //+nice: intentionally use as global var
    fmt.Println(i, *j) //11 21
}
func main() {
    i := 10 //scope: main
    j := 20
    fun(i, &j)
    fmt.Println(i, j) //10 21
}

using short instructions for internal operators:

package main
import "fmt"
func main() {
    i := 10 //scope: main
    j := 4
    for i := 'a'; i < 'b'; i++ {
        fmt.Println(i, j) //97 4
    }
    fmt.Println(i, j) //10 4

    if i := "test"; len(i) == j {
        fmt.Println(i, j) // i= test , j= 4
    } else {
        fmt.Println(i, j) //test 40
    }
    fmt.Println(i, j) //10 4
}

Global Varnish Shading:

package main
import "fmt"
var i int = 1 //global
func main() {
    j := 2
    fmt.Println(i, j) //1 2
    i := 10           //Shadowing global var
    fmt.Println(i, j) //10 2
    fun(i, j)         //10 2
}
func fun(i, j int) {
    //i := 100   //no new variables on left side of :=
    fmt.Println(i, j) //10 2
}
+1
source

All Articles