dqm83011 2016-01-09 07:48
浏览 609
已采纳

无法在Golang中使用单引号分配字符串

I am learning go and when playing with string I noticed that if a string is in single quotes then golang is giving me an error but double quotes are working fine.

func main() {
    var a string
    a = 'hello' //will give error
    a = "hello" //will not give error
}

This is the error I get on my system:

illegal rune literal

While when I try to do the same on playground I am getting this error:

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 '

I am not able to understand the exact reason behind this as in for example Python, Perl one can declare a string with both single and double quote.

  • 写回答

2条回答 默认 最新

  • dongpaozhi5734 2016-01-09 07:59
    关注

    In Go, '⌘' represents a single character (called a Rune), whereas "⌘" represents a string containing the character .

    This is true in many programming languages where the difference between strings and characters is notable, such as C++.

    Check out the "Code points, characters, and runes" section in the Go Blog on Strings

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊