如何在 Golang 中为字符串添加单引号?

EB2*_*127 3 string quotes go strconv

也许这是一个简单的问题,但我还没有想出如何做到这一点:

我在 Go 中有一个字符串切片,我想将其表示为逗号分隔的字符串。这是切片example

example := []string{"apple", "Bear", "kitty"}
Run Code Online (Sandbox Code Playgroud)

我想将其表示为带单引号的逗号分隔字符串,即

'apple', 'Bear', 'kitty'
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何在 Go 中有效地做到这一点。

例如,strings.Join()给出一个逗号分隔的字符串:

commaSep := strings.Join(example, ", ")
fmt.Println(commaSep)
// outputs: apple, Bear, kitty
Run Code Online (Sandbox Code Playgroud)

关闭,但不是我需要的。我也知道如何添加双引号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"
Run Code Online (Sandbox Code Playgroud)

再次,不是我想要的。

如何输出字符串'apple', 'Bear', 'kitty'

pho*_*ter 10

下面的代码怎么样?

commaSep := "'" + strings.Join(example, "', '") + "'"
Run Code Online (Sandbox Code Playgroud)

去游乐场