Go中的变量init

I see following code (I simplified it a bit).

func getEndpoints(db *sqlx.DB) s.Endpoints {
    var endpoints s.Endpoints
    {
        repository := acl.NewRepository(db)
        service := stat.NewService(repository)
        endpoints = s.Endpoints{
            GetEndpoint: s.MakeEndpoint(service),
        }
    }

    return endpoints 
}

If I understand this code correctly, code inside var endpoints s.Endpoints{...} is executed line by line and endpoints = s.Endpoints ... line initialises var endpoints variable declared above.

I suppose that it's correct to rewrite it like this (correct me if I'm wrong):

func getEndpoints(db *sqlx.DB) s.Endpoints {

    repository := acl.NewRepository(db)
    service := stat.NewService(repository)
    endpoints := s.Endpoints{
        GetEndpoint: s.MakeEndpoint(service),
    }

    return endpoints 
}

So can somebody explain me why initialisation is written inside var endpoints s.Endpoints{...}. Is there any idea to do it like this? Am I missing something?

Adding a new block will create a new variable scope, and variables declared in that block will not be available outside of it:

var endpoints s.Endpoints
{
    repository := acl.NewRepository(db)
    service := stat.NewService(repository)
    endpoints = s.Endpoints{
        GetEndpoint: s.MakeEndpoint(service),
    }
}

// service and repository  variables are not defined here!

In your specific simplified example it makes little sense, but if you have other blocks with the same variables it makes more sense. For example:

var endpoints s.Endpoints
{
    repository := acl.NewRepository(db)
    service := stat.NewService(repository)
    endpoints = s.Endpoints{
        GetEndpoint: s.MakeEndpoint(service),
    }
}

 // Get another repository
{
    repository := otherRepo.NewRepository(db)
    repository.DoSomething()
}

Some people consider this "good hygiene". Personally, I don't think it's worth the decrease in readability.

They are equivalent.

The {...} block has nothing to do with the variable declaration with the var keyword. It just happens to be written one after the other.

The {...} is a simple block, nothing else. The var declaration does not require that block, and even if there is one, it is not related to the variable declaration. You can insert a block wherever you would insert a statement.

The rare case when an explicit block is used (when there isn't required one) is to group statements, and to control the scope of the variables and other identifiers declared inside them, because the scope of the variables end at the end of the innermost containing block (Spec: Declarations and Scope).