Skip to content

cmd/vet: reporting "composite literal uses unkeyed fields" for slice types #9171

Closed
@fsouza

Description

@fsouza
I have a typed defined from a slice (type TemplateDataList []TemplateData), and when I
create instances of this type

% go version
go version devel +ffe33f1f1f17 Tue Nov 25 15:41:33 2014 +1100 darwin/amd64
% cd $GOPATH/src/golang.org/x/tools
% hg tip
changeset:   1264:e105dadc3014
tag:         tip
user:        Andrew Gerrand <[email protected]>
date:        Wed Nov 26 15:31:30 2014 +1100
summary:     x/tools/cmd/godoc: add golang.org/x/oauth2 import path
% cd -
% cat code_bad.go
package main

import (
    "fmt"
    "github.com/tsuru/tsuru/iaas"
)

type Template struct {
    Name string
    Data iaas.TemplateDataList
}

func main() {
    template := Template{
        Name: "My Template",
        Data: iaas.TemplateDataList{
            {Name: "color", Value: "red"},
            {Name: "size", Value: "100"},
        },
    }
    fmt.Println(template)
}
% go vet code_bad.go
code_bad.go:16: iaas.TemplateDataList composite literal uses unkeyed fields
exit status 1

Here's where TemplateDataList is defined:
https://github.com/tsuru/tsuru/blob/0ac00f9d08f67efa39b679b643f74ff6ebba8530/iaas/template.go#L21

Now, if I copy the TemplateDataList definition to the same file, the error disappears:

% cat code_good.go
package main

import (
    "fmt"
    "github.com/tsuru/tsuru/iaas"
)

type TemplateDataList []iaas.TemplateData

type Template struct {
    Name string
    Data TemplateDataList
}

func main() {
    template := Template{
        Name: "My Template",
        Data: TemplateDataList{
            {Name: "color", Value: "red"},
            {Name: "size", Value: "100"},
        },
    }
    fmt.Println(template)
}
% go vet code_good.go
# no failures

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions