I'm working on structs, methods, and interfaces in Go, and am writing some code to test the concepts. I'm stuck on the following concept in an example I'm creating - some amplifiers have preamp tubes and power tubes. I thought I could define them in the amp struct using a generic tube struct, but of course it doesn't work the way I have it written, and when I research nested structs, they don't seem to be the applicable concept. How do I structure this so that "amp" has "preamptubes" and "powertubes", and those are each type "tube"?
type tube struct {
model string
number int8
}
type amp struct {
name string
model string
manufacturer string
color string
knobs int8
switches int8
jacks int8
preamptubes tube
powertubes tube
ouputpower int8
fxloop bool
}
**Edit: Update:** I should have including the error I was getting in the question. I rewrote the package this morning, and the preamptubes and powertubes types now work as expected. I suspect the problem was with how I was instantiating them in the main function. The following now works. This isn't production code, just an exercise to practice concepts. Thanks for helping me take a fresh look at it.package main
import "fmt"
type tube struct {
model string
number int8
}
type amp struct {
name string
model string
manufacturer string
color string
knobs int8
switches int8
jacks int8
preamptubes tube
powertubes tube
ouputpower int8
fxloop bool
}
func main() {
a := amp{
name: "MegaAmp",
model: "MA9000",
manufacturer: "Amps R Us",
color: "blonde",
knobs: 9,
switches: 5,
jacks: 6,
preamptubes: tube{
model: "12AX7",
number: 3},
powertubes: tube{
model: "6V6",
number: 4},
fxloop: true}
fmt.Println(a)
fmt.Println(a.preamptubes.model)
}
I may be misunderstanding, but are you asking for a nested slice of structs? To accomplish this, simply use []tube
:
package main
import "fmt"
type tube struct {
model string
number int8
}
type amp struct {
name string
model string
manufacturer string
color string
knobs int8
switches int8
jacks int8
preamptubes []tube
powertubes []tube
ouputpower int8
fxloop bool
}
func main() {
v := amp{
name: "da amp",
preamptubes: []tube{{model: "A"}},
powertubes: []tube{{model: "B"}},
}
fmt.Printf("%+v
", v)
}
https://play.golang.org/p/bPGHuEO40D0
Just a shot in the dark here, but you might also be running into import
issues if your values exist in separate packages. Changing the first letter to an uppercase (ie, Tube
, and AMP
[see https://github.com/golang/go/wiki/CodeReviewComments#initialisms)) to access structs from different packages.
It's not clear what exactly are you trying to achieve (what's the context / large picture here?), but consider this:
You can add another flag to the tube to designate its type, then have an array of types
type tubekind int
const (
preamptube tubekind = iota
powertube
)
type tube struct {
model string
number int8
kind tubekind
}
type amp struct {
tubes []tube
// ... other fields
}
You can create a tube this way:
tube{"model", 1, powertube}
tube{"model2", 2, preamptube}