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

在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条)

报告相同问题?