2 回答
TA贡献1799条经验 获得超9个赞
在循环中迭代并将键附加到字符串:
package main
import "fmt"
func main() {
var userinputmap = make(map[string]string)
userinputmap["key1"] = "value1"
userinputmap["key2"] = "value2"
userinputmap["key3"] = "value3"
output :=""
for key,_ := range userinputmap {
output +=(key+",")
}
output = output[:len(output)-1]
fmt.Println(output)
}
TA贡献1876条经验 获得超7个赞
另一种选择是将所有密钥收集到一个切片中,然后使用strings.Join方法将它们连接起来。
package main
import "fmt"
import "strings"
func main() {
var userinputmap = make(map[string]string)
userinputmap["key1"] = "value1"
userinputmap["key2"] = "value2"
userinputmap["key3"] = "value3"
keys := []string {}
for k, _ := range userinputmap {
keys = append(keys, k)
}
keys_string := strings.Join(keys, ",")
fmt.Println(keys_string)
}
- 2 回答
- 0 关注
- 159 浏览
添加回答
举报