With a simple for
loop:
for _, v := range myconfig {
if v.Key == "key1" {
// Found!
}
}
Note that since element type of the slice is a struct
(not a pointer), this may be inefficient if the struct type is "big" as the loop will copy each visited element into the loop variable.
It would be faster to use a range
loop just on the index, this avoids copying the elements:
for i := range myconfig {
if myconfig[i].Key == "key1" {
// Found!
}
}
Notes:
It depends on your case whether multiple configs may exist with the same key
, but if not, you should break
out of the loop if a match is found (to avoid searching for others).
for i := range myconfig {
if myconfig[i].Key == "key1" {
// Found!
break
}
}
Also if this is a frequent operation, you should consider building a map
from it which you can simply index, e.g.
// Build a config map:
confMap := map[string]string{}
for _, v := range myconfig {
confMap[v.Key] = v.Value
}
// And then to find values by key:
if v, ok := confMap["key1"]; ok {
// Found
}