简体   繁体   中英

Formatting a map[] in golang

I have a list of hosts inbound in the form of one string separated by commas.

EXAMPLE: "host01,host02,host03,"

I have this line that was an array of strings but I need it to be a map[string]interface{}

Here is what it is how do I make it a map[string]interface{} ?

• Removing the trailing or any trailing comma.

hosts := []string{strings.TrimSuffix(hostlist, ",")}

• Later I split them on the comma like this.

hosts = strings.split(hosts[0], ",")

I just need to make it so names are keys and the values are unknown from APIs so an interface{} .

Thanks and forgive me I know this is super simple I am just not seeing it.

Loop over your slice of strings. Set each map entry to nil.

There is no fancy syntax like Python's list comprehensions or Perl's freaky group assignments.

And remember that StackOverflow's tag info is often really useful. See: https://stackoverflow.com/tags/go/info

And from there to the language specification. One bit that will help is https://golang.org/ref/spec#For_range if you aren't familiar with Go's for syntax to loop over slices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM