如何在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","kitty"

再次,不是我想要的。

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

解决方法

下面的代码怎么样?

commaSep := "'" + strings.Join(example,"','") + "'"

Go Playground