dqwn64004 2019-01-09 02:34
浏览 88

如何在Go中嵌套结构

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

  • 写回答

2条回答 默认 最新

  • dongyupen6269 2019-01-09 02:43
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度