Given a URL like the following:
http://127.0.0.1:3001/find?fields=hostname,App,Node_type,invalid
I extract fields into a slice like this:
filters := r.URL.Query().Get("fields")
fmt.Println(filters)
Result:
hostname,App,Node_type,invalid
It is received as a string, but I'd prefer to separate the substrings into a sequence.
I think your URL should be
http://127.0.0.1:3001/find?fields=hostname&fields=App&fields=Node_type&fields=invalid
or if you don't like that, you can parse
filterSlice:=strings.Split("filters", ",")
The question actually concerns how to split a string on a particular delimiter. For that, you can use the strings.Split()
function:
import "strings"
// ...
filters := strings.Split(r.URL.Query().Get("fields"), ",")
Your filters
variable will now be a slice, which may be empty if there was no "fields" query parameter available.