doutong2132 2017-08-27 15:31
浏览 9

如何使用将多个结构连接到同一结构

I'm trying to play around with recursive structs, where when I have multiple I can add them together, creating a new struct with those embedded. However, I'm not sure what the proper way to approach this is.

I've included a code snippet below to further illustrate what I mean.

package main

import "fmt"

type Container struct {
    F          int
    Collection []SubContainer
}

type SubContainer struct {
    Key    string
    Value  int
}

func main() {
    commits := map[string]int{
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
    }

    sc := []SubContainer{}
    c := []Container{}
    count := 0

    for k, v := range commits {
        sc = append(sc, SubContainer{Key: k, Value: v})
        count++

        if len(sc) == 2 {
            c = append(c, Container{Collection: sc, F: count})
            sc = nil
        }
    }

    for _, r := range c {
        fmt.Println(r)
    }
}

Result:

{2 [{a 1} {b 2}]}
{4 [{c 3} {d 4}]}

Desired result:

{6 {2 [{a 1} {b 2}]} {4 [{c 3} {d 4}]}}

Playground link: https://play.golang.org/p/j6rbhgcOoT

One caveat I'm having trouble wrapping my head around is that the commits length may change (I was initially thinking I could just create a different parent struct). Any suggestions would be appreciated... Is doing this somehow with recursive structs the right approach to accomplish this? Thanks!

  • 写回答

2条回答 默认 最新

  • douwen4401 2017-08-27 16:29
    关注

    I attempted to get close to the desired output without being sure about the exact goal, you will find below a modified version of the snippet you provided.

    You can use the String() string method on a collection to customize the format.

    package main
    
    import "fmt"
    
    type ContainerCollection struct {
        Count int
        List  []Container
    }
    
    func (cc ContainerCollection) String() string {
        total := 0
        for _, c := range cc.List {
            total += c.F
        }
        return fmt.Sprintf("{%d %v}", total, cc.List)
    }
    
    type Container struct {
        F          int
        Collection []SubContainer
    }
    
    type SubContainer struct {
        Key   string
        Value int
    }
    
    func main() {
        commits := map[string]int{
            "a": 1,
            "b": 2,
            "c": 3,
            "d": 4,
        }
    
        c := ContainerCollection{Count: 0}
        sc := []SubContainer{}
    
        for k, v := range commits {
            sc = append(sc, SubContainer{Key: k, Value: v})
            c.Count++
            if len(sc) == 2 {
                c.List = append(c.List, Container{Collection: sc, F: c.Count})
                sc = []SubContainer{}
            }
        }
        // Should also cover odd number of commits
        if len(sc) != 0 {
            c.Count++
            c.List = append(c.List, Container{Collection: sc, F: c.Count})
        }
    
        // for _, r := range c.List { fmt.Println(r) }
    
        fmt.Println(c)
    }
    

    Result:

    {6 [{2 [{a 1} {b 2}]} {4 [{c 3} {d 4}]}]}
    

    Playground

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大