如何在golang中用逗号分隔值打印Slice [英] How to print Slice with comma seperated values in golang
本文介绍了如何在golang中用逗号分隔值打印Slice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入:
// a slice of type string.
data := []string{"one", "two", "three"}
预期输出:
["one","two","three"]
我尝试过使用这些格式说明符 https://play.golang.org/p/zBcFAh7YoVn
fmt.Printf("%+q\n", data)
fmt.Printf("%#q\n", data)
fmt.Printf("%q\n", data)
// using strings.Join()
result := strings.Join(data, ",")
fmt.Println(result)
输出:所有值都没有逗号
["one" "two" "three"]
[`one` `two` `three`]
["one" "two" "three"]
one,two,three
推荐答案
// define slice
args := []string{"one", "two", "three"}
// prepend single quote, perform joins, append single quote
output := "'"+strings.Join(args, `','`) + `'`
fmt.Println(output)
去游乐场: https://play.golang.org/p/pKu0sO_QsGo
这篇关于如何在golang中用逗号分隔值打印Slice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文