Golang: String

By Xah Lee. Date: . Last updated: .

Intepreted String Literal

String syntax is like this:


package main

import "fmt"

func main() {

	var x = "abc and ♥"

	// 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

Returns the nth byte of string s.
Index start at 0.
The return value's type is byte. [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

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 = "♥"
	// 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


Golang String


Compile and Run


Types and Values

Branching and Loop

Data Structure