composite literal composite literal uses unkeyed fields

@pixabay

Story

Recently I am working for a company as a Golang developer. I Involved in development in there and the developers choice Google App Engine in Platform. One day I try to deploy my source codes to App Engine, and I see an error 'composite literal uses unkeyed fields.'

The codes

It happened in the following codes.

func NewNullString(s string) NullString {
    return NullString{sql.NullString{s, s != ""}}
}

So I fixed the codes like this. It resolved the problem.

func NewNullString(s string) NullString {
    return NullString{sql.NullString{
        String: s,
        Valid:  s != "", 
    }}
}

Please let me explain what happen above codes.

The reason

I can run compiling the codes using unkeyed fields on my machine. But I cannot deploy it to App Engine. The compiling is not a problem. The reason is go-vet reporting suspicious constructs before compiling. It can be reproduced following codes.

% go tool vet model/model.go
model/model.go:31: database/sql.NullString composite literal uses unkeyed fields

By the way we can specify option to ignore the warnings.

% go tool vet --composites=false model/model.go # no reporting
% go tool vet --composites=true  model/model.go # reporting
model/model.go:31: database/sql.NullString composite literal uses unkeyed fields

Enjoy