douyou7072 2013-10-11 16:17
浏览 972
已采纳

在Go数组中查找所有匹配项

I have an array of structs (struct detailed at bottom)

I want to find all the structs that match certain values for, example, leg and site.

So if leg=101 and site=1024A give back all the structs that match these criteria.

What is the Go manner for doing this?

type JanusDepth struct {
    dataset string
    ob      string
    leg     string  
    site    string  
    hole    string
    age     float64
    depth   float64
    long    float64
    lat     float64
}
  • 写回答

2条回答 默认 最新

  • 普通网友 2013-10-11 16:25
    关注

    Dead simple:

    leg      := "101"
    site     := "1024A"
    filtered := []JanusDepth{}
    
    for _, e := range MyArrayOfStructs {
        if(e.leg == leg && e.site == site) {
            filtered = append(filtered, e)
        }
    }
    
    // filtered contains your elements
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料