无法在golang中为单引号分配字符串

shi*_*ams 30 string go

我正在学习去和玩字符串时我注意到如果一个字符串是单引号,那么golang给我一个错误,但双引号工作正常.

func main() {
    var a string
    a = 'hello' //will give error
    a = "hello" //will not give error
}
Run Code Online (Sandbox Code Playgroud)

这是我在我的系统上遇到的错误:

illegal rune literal
Run Code Online (Sandbox Code Playgroud)

当我尝试在操场上做同样的事情时,我收到此错误:

prog.go:9: missing '
prog.go:9: syntax error: unexpected name, expecting semicolon or newline or }
prog.go:9: newline in string
prog.go:9: empty character literal or unescaped ' in character literal
prog.go:9: missing '
Run Code Online (Sandbox Code Playgroud)

我无法理解这背后的确切原因,例如Python,Perl可以声明一个包含单引号和双引号的字符串.

ti7*_*ti7 63

在Go中,'?'表示单个字符(称为符文),而"?"表示包含该字符的字符串?.

在许多编程语言中都是如此,其中字符串和字符之间的区别是显着的,例如C++.

查看字符串Go Blog中的"代码点,字符和符文"部分

  • 您还可以使用`\`blah \``返回刻度来表示原始字符串. (13认同)