Golang: String
Intepreted String Literal
String syntax is like this:
"abc"
package main import "fmt" func main() { var x = "abc and ♥" fmt.Println(x) // abc and ♥ }
String can contain Unicode character, example: ♥ (U+2665: BLACK HEART SUIT)
Any character can appear between the "double quotes", except the quote itself or newline character.
To include newline, use \n
.
To include a quote character, use \"
, example: "the \"thing\""
Backslash Escapes
Golang: String Backslash Escape
Raw String Literal
If you don't want backslash to have special meaning, use ` (U+60: GRAVE ACCENT) to quote the string.
var x = `long text`
Anything can appear inside except the grave accent char itself.
And, carriage return character (Unicode codepoint 13) in it is discarded.
If you run the command line tool gofmt
, it will remove carriage return.
package main import "fmt" var x = `long text many lines tab too` func main() { fmt.Printf("%v\n", x) }
String Index
s[n]
-
Returns the nth byte of string s.
Index start at 0.
The return value's type isbyte
. [see Golang: Basic Types] If the string contains ASCII characters only, then index corresponds to characters. [see ASCII Characters ] If the string contains non-ASCII characters, index does not corresponds to characters. (If you want to work with string as characters not bytes, you need to convert it to rune slice. See Golang: String, Byte Slice, Rune Slice)
package main import "fmt" func main() { var x = "abc" fmt.Printf("%#v\n", x[0]) // 0x61 fmt.Printf("%#v\n", x[1]) // 0x62 fmt.Printf("%#v\n", x[2]) // 0x63 }
String is a Sequence of Bytes
- Golang string is a sequence of Bytes.
- Each character in string is converted to bytes. If the character is ASCII Character, then it is 1 byte. But if it is non-ASCII char, it is 1 to 4 bytes by UTF-8 encoding. [see Unicode Basics: Character Set, Encoding, UTF-8]
- Go string can contain any Unicode character, but stored as bytes.
- String can store any byte sequence, and can contain byte sequences that is not valid encoding of any character. You can create a string of any byte by using the hexadecimal escape
\xhh
. [see Golang: String Backslash Escape]
package main import "fmt" // character A has codepoint 65 in decimal , and 41 in hexadecimal. So, "A" and "\x41" creates the same string. func main() { fmt.Printf("%v\n", "A" == "\x41") // true }
package main import "fmt" // showing how string is byte sequence, not char sequence func main() { // string of a single char var x = "♥" // name: BLACK HEART SUIT // codepoint 9829, hexadecimal 2665 // utf8 encoding: E2 99 A5 // length fmt.Printf("%v\n", len(x)) // 3 // first byte in hexadecimal fmt.Printf("%x\n", x[0]) // e2 }