string - 如何在 Golang 中为字符串添加单引号?
问题描述
也许这是一个简单的问题,但我还没有弄清楚如何做到这一点:
我在 Go 中有一个字符串切片,我想将其表示为逗号分隔的字符串。这是切片example
:
example := []string{"apple", "Bear", "kitty"}
我想把它表示为一个带单引号的逗号分隔字符串,即
'apple', 'Bear', 'kitty'
我无法弄清楚如何在 Go 中有效地做到这一点。
例如,strings.Join()
给出一个逗号分隔的字符串:
commaSep := strings.Join(example, ", ")
fmt.Println(commaSep)
// outputs: apple, Bear, kitty
关闭,但不是我需要的。我也知道如何添加双引号strconv
,即
new := []string{}
for _, v := range foobar{
v = strconv.Quote(v)
new = append(new, v)
}
commaSepNew := strings.Join(new, ", ")
fmt.Println(commaSepNew)
// outputs: "apple", "Bear", "kitty"
再次,不是我想要的。
如何输出字符串'apple', 'Bear', 'kitty'
?
解决方案
推荐阅读
- dataset - 如何在 H2O 的无人驾驶 AI 中格式化数据集以进行时间序列预测
- thymeleaf - Thymeleaf 删除代码块
- haskell - GHC 未消除的内循环构造函数
- asp.net-core - Visual Studio 2017 前缀文件嵌套
- windows - Windows 批处理脚本中的通配符 *
- javascript - html多个重定向取决于输入
- python - 从字符串列创建虚拟变量
- angular - 将 rootreducer 传递给 configureStore 方法编译失败
- javascript - 匹配自动完成字符串的任何部分,而不仅仅是第一个字母
- c - fgets 如何处理 \n?