type Apis struct {
Items []struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreatedDate int `json:"createdDate"`
APIKeySource string `json:"apiKeySource"`
EndpointConfiguration struct {
Types []string `json:"types"`
} `json:"endpointConfiguration"`
} `json:"items"`
}
This the struct I have defined to store the APIs i get in json format. How do I get a specific API by its name and then get it's ID. For example lets say, apiname == Shopping and i want Shopping API's ID assigned to id variable.
ps : I'm new to golang and a well explained answer will be very much appreciated. Thanks guys
In your case
Itemsis slice of custom structs, so you have to perform search over loop, like this:It would be better to have
mapof custom structs instead of slice, so you could to do something like this:IMPORTANT: with slice you complexity of algorithm will be
O(n)with map -O(1)which is way better (it's best what possible).