Please help me explain this syntax

Please can someone explain the below syntax to me

x := 1
fmt.Println(x)
{ // Why do we have a bracket here?
    fmt.Println(x)
    x += 2
    fmt.Println(x)
} // And closing here?
fmt.Println(x)

The variable x is just here as part of a code i coped from another source, i just need to understand the bracket after the fmt.Printf(x) because according to golang official doc, fmt.Println returns (n int, err error).

I think the original code was trying to demonstrate that a new x can be declared inside the scope, between the curly braces, without affecting the outer x. But this sample doesn’t declare a new x so there is no reason to introduce that confusing new block.

1 Like

the code should be like that to illustrate something :wink:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.