Home >Backend Development >Golang >Why is 'hello' Illegal as a String Declaration in Go?
Understanding String Declarations in Go: Why 'hello' is Illegal
In Go, strings can be declared using either double or single quotes, but with a subtle difference. While double quotes ("hello") represent a string containing characters, single quotes ('hello') represent a single Unicode code point or rune.
When you assign 'hello' to a string variable in Go, the compiler interprets it as a rune literal. A rune is a single Unicode code point, not a string of characters. As a result, attempting to assign a single character to a string variable causes an "illegal rune literal" error.
The difference between strings and runes is significant in Go. Strings are composed of runes, but not all runes can be represented as strings. For example, the null character (
The above is the detailed content of Why is 'hello' Illegal as a String Declaration in Go?. For more information, please follow other related articles on the PHP Chinese website!