what happen inside curly bracket in golang

Viewed 1984

anyone know what happen to this code, i get it when copy code to build golang micro service

var logger log.Logger
{
    logger = log.NewLogfmtLogger(os.Stderr)
    logger = log.NewSyncLogger(logger)
    logger = log.With(logger,
        "service", "movie",
        "time:", log.DefaultTimestampUTC,
        "caller", log.DefaultCaller,
    )
}

my question is what happen to inside curly bracket ?

var number int 
{

   . . . . . . 

}
1 Answers

Curly brackets on their own define lexical scope in Go. Meaning, whatever happens in that scope, stays in that scope. Variable declarations, calculations, etc.

That said, it can drag the scope in from outside. Meaning if you declare a variable, like number outside, then modify said number in the curly brackets, you will see it modified.

Here is a post to understand scope and blocks: https://medium.com/golangspec/scopes-in-go-a6042bb4298c

Related