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
}
In Go language, arrays are mutable, so that you can use array [index] syntax to the left-hand side of the assignment to set the elements of the array at the given index. You can access the elements of the array by using the index value or by using for loop. In Go language, the array type is one-dimensional.
finditer method The re.finditer () works exactly the same as the re.findall () method except it returns an iterator yielding match objects matching the regex pattern in a string instead of a list. It scans the string from left-to-right, and matches are returned in the iterator form. Later, we can use this iterator object to extract all matches.
In an array, you are allowed to store zero or more than zero elements in it. The elements of the array are indexed by using the [] index operator with their zero-based position, means the index of the first element is array [0] and the index of the last element is array [len (array)-1]. In Go language, arrays are created in two different ways:
So let’s dive deeper into the concept of matching in GoLang. regexp (regular expressions) is all about string/pattern matching. Every function, every part of regexp functions somewhere requires text matching.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With