ijosalfar
(ijosalfar)
January 27, 2021, 9:13am
1
I bit confuse to filter values with some conditional returns, I have this response, that I parsed from grafana request. How can I bring out only if values > 25?
Thank you In advanced
{
"ha-external-01":45.804,
"twem-01":20.800243,
"redis-01":20.8902,
"redis-03":20.89015,
"arrango-01":20.8890865
}
expected
{"ha-external-01":45.804}
ermanimer
(erman imer)
January 27, 2021, 11:05am
2
Here is a simple solution to give you an idea.
https://play.golang.org/p/5uP1Xuf1rqm
shirejoni
(Shirejoni)
January 27, 2021, 6:13pm
3
Hi
@ermanimer ’s solution is good and complete but i think if your response json’s key is dynamic the solution is like this:
https://play.golang.org/p/l8KNgQhByo0
i just change some line of @ermanimer ’s solution
ijosalfar
(ijosalfar)
February 8, 2021, 6:13am
5
Hi @shirejoni , apologies for the late reply. How can I return values without map[] map[] notation eg
{"ha-external-01":45.804, "redis-b-03":35.3342, "kafka-a-01":47.54754 }
Eg my return values:
map[]
map[redis-b-03:0.323432]
map[docker-a-01:0.652938221 neo-redis-b-03:0.300601]
map[kafka-a-01:0.4754754 docker-a-01:0.6529382 redis-b-03:0.30060120]
NobbZ
(Norbert Melzer)
February 8, 2021, 7:15am
6
Not sure what you are asking, but map[…] is just a debugging representation. It is not “returned”, neither do you need to return values “without it”.
ijosalfar
(ijosalfar)
February 16, 2021, 4:39am
7
Hi @NobbZ ,
I mean how to get one single return values like this not below:
[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501 mysql-b-02:61.23623409094686]
map[ha-external-a-01:59.48262787184605]
map[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501]
map[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501 mysql-b-02:61.23623409094686]
My snippet code based on @ermanimer
filtered = make(map[string]float64)
for key, value := range response {
if value > 55 {
filtered[key] = value
fmt.Println(filtered)
}
}
NobbZ
(Norbert Melzer)
February 16, 2021, 6:28am
8
You are not returning anything, just printing out stuff. You do this each time your filter condition was true.
You can return values using the return statement.
Though its not clear to me whether you want to return the map after filtering the input map, or just the first encountered value.
ijosalfar
(ijosalfar)
February 17, 2021, 10:38am
9
Apologies for the basic question , I mean how to get a list of values like this :
[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501 mysql-b-02:61.23623409094686]
I have figured out using append, but it only append 1 by 1 values:
filtered = make(map[string]float64)
var out []string
for key, value := range response {
if value > 55 {
filtered[key] = value
// fmt.Println(key, value)
out = append(out, key)
fmt.Println(out)
}
}
}
clbanning
(Charles Banning)
February 17, 2021, 12:40pm
10
ijosalfar
(ijosalfar)
February 19, 2021, 7:57am
11
Thanks for your reply @clbanning , my purpose is sending filtered values using SMTP Gmail. I got some errors like this while calling send func() inside message []byte :
message := []byte("Subject:" + subject + "\n\n" +
"CPU with the condition over than 80%: " +
fmt.Sprintf(`{"items":"%v"}`, checklink))
My error:
checkURLS(links) used as value
too many arguments to return
have (string)
want ()
any suggestion? thanks for your help. My plain full code on this https://play.golang.org/p/po43WuzhGzC
clbanning
(Charles Banning)
February 19, 2021, 1:29pm
12
https://play.golang.org/p/p_X-ABNkTae
You’ve got typos, don’t use limit, and should handle errors. In the end, the message is fine.