missing type in composite literal

9,242 views
Skip to first unread message

Tong Sun

unread,
Mar 3, 2013, 12:20:28 PM3/3/13
to golan...@googlegroups.com
Hi, 

I don't know how to fix this "missing type in composite literal" problem. 

package main

import "fmt"

type Room struct {
numOfLamps int
}

type Livingroom struct {
Room
items map[string]int
}

func main() {
r := Room{5}
pr := &r

l := Livingroom{*pr, {"sofa": 2, "chair": 4}}
fmt.Printf("%#v\n", l)
}

please help. 

Thanks

peterGo

unread,
Mar 3, 2013, 12:37:16 PM3/3/13
to golan...@googlegroups.com
Add a type for items:

type Items map[string]int

Then use it in the literal:

l := Livingroom{*pr, Items{"sofa": 2, "chair": 4}}

Peter

peterGo

unread,
Mar 3, 2013, 12:39:07 PM3/3/13
to golan...@googlegroups.com

Tong Sun

unread,
Mar 3, 2013, 12:57:14 PM3/3/13
to peterGo, golang-nuts
Thanks Peter, 

I know adding a type for items will work, but the problem is, what's the proper way to do it without adding that extra type?

On Sun, Mar 3, 2013 at 12:37 PM, peterGo <go.pe...@gmail.com> wrote:
Add a type for items:

type Items map[string]int

Then use it in the literal:

l := Livingroom{*pr, Items{"sofa": 2, "chair": 4}}

Peter

Kyle Lemons

unread,
Mar 3, 2013, 1:07:35 PM3/3/13
to Tong Sun, peterGo, golang-nuts
Specify the real type: map[string]int{"foo": 42}


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tong Sun

unread,
Mar 3, 2013, 1:42:23 PM3/3/13
to Kyle Lemons, peterGo, golang-nuts
Ah, this way. Thanks a lot. 
Reply all
Reply to author
Forward
0 new messages