符文文字go语言中的多个字符
我有一个字符串MyString,我想在这个数据中附加如下内容:
MYString ("1", "a"), ("1", "b") //END result
我的代码是这样的:
query := "MyString";
array := []string{"a", "b"}
for i , v := range array{
id := "1"
fmt.Println(v,i)
query += '("{}", "{}"), '.format(id, v)
}
但我收到两个错误:
./prog.go:15:23: more than one character in rune literal
./prog.go:15:39: 'u0000'.format undefined (type rune has no field or method format)
回答
在 Go 中不能对字符串使用单引号。您只能使用双引号或反引号。单引号用于单个字符,称为符文
将您的线路更改为:
query += "("{}", "{}"), ".format(id, v)
或者
query += `("{}", "{}"), `.format(id, v)
然而,Go 不是 python。Go 没有这样的format方法。但它有fmt.Sprintf。
所以要真正修复它,请使用:
query = fmt.Sprintf(`%s("%s", "%s"), `, query, id, v)