Ubuntu Pastebin

Paste from Jamie Strandboge at Tue, 28 Jun 2016 12:44:45 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from info_snap_yaml.go:

type slotYaml struct {
	Interface string                 `yaml:"interface"`
	Attrs     map[string]interface{} `yaml:"attrs,omitempty"`
	Apps      []string               `yaml:"apps,omitempty"`
	Label     string                 `yaml:"label"`
}


Example yaml:
slots:
  foo:
    interface: bar
    baz:
    - norf
    - qux
    quux:
    - corge


This is fine (gets 'baz' and 'quux'):
for attr := range slot.Attrs {
    ...
}


Can see the data with:
fmt.Printf("%#+v\n", slot.Attrs)
fmt.Printf("%#+v\n", slot.Attrs["baz"])

eg:
map[string]interface {}{"baz":[]interface {}{"norf", "corge"}}
[]interface {}{"norf", "corge"}


None of these work to get at 'norf' and 'qux':
fmt.Printf("%d\n", len(slot.Attrs["baz"]))
invalid argument slot.SlotInfo.Attrs["baz"] (type interface {}) for len


fmt.Printf("%d\n", len(slot.Attrs["baz"].(string)))
... Panic: interface conversion: interface is []interface {}, not string (PC=0x45A35E)


for p := range slot.Attrs["baz"] {
    fmt.Printf("%s\n", p.(string))
}
cannot range over slot.SlotInfo.Attrs["baz"] (type interface {})


for i, p := range slot.Attrs["baz"] {
    fmt.Printf("%s\n", p.(string))
}
cannot range over slot.SlotInfo.Attrs["baz"] (type interface {})


for p := range slot.Attrs["baz"].([]string) {
    fmt.Printf("%s\n", p)
}
... Panic: interface conversion: interface is []interface {}, not []string (PC=0x45A35E)


for p := range slot.Attrs["baz"].(string) {
    fmt.Printf("%s\n", p)
}
... Panic: interface conversion: interface is []interface {}, not string (PC=0x45A35E)


AIUI I cannot assert '.([]string)' with interfaces for '[]interface{}' and
instead must assert each individual 'interface{}', but I have no way to iterate
over '[]interface{}'. Note: I can go from '[]string' to '[]interface{}' just
fine, but can't go from '[]interface{}' to '[]string'.

What am I missing?
Download as text