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)
}