输入: // a slice of type string. data := []string{"one", "two", "three"}预期输出:["one","two","three"]我尝试使用这些格式说明符 https://play.golang.org/p/zBcFAh7YoVnfmt.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
3 回答
白衣非少年
TA贡献1155条经验 获得超0个赞
// 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
jeck猫
TA贡献1909条经验 获得超7个赞
JSON 生成一个很好的转义 csv
https://go.dev/play/p/Qgh2WgOvAUW
data := []string{"1", "2", "A", "B", "Hack\"er"}
b, _ := json.Marshal(data)
fmt.Printf("%v", string(b)) // ["1", "2", "A", "B", "Hack\"er"]
Join如果字符串包含双引号,则仅使用会创建损坏的值。
白板的微信
TA贡献1883条经验 获得超3个赞
data := []string{"1", "2"}
var result string
if len(data) > 0 {
result = "\"" + strings.Join(data, "\",\"") + "\""
}
fmt.Println(result)
- 3 回答
- 0 关注
- 733 浏览
添加回答
举报
0/150
提交
取消