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
}
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
If your data is ordered on one key, then you can use http://golang.org/pkg/sort/#Search to do a binary search, which is better for performance if the amount of data is moderate to large.