Golang: String

By Xah Lee. Date: . Last updated: .

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, e.g. (U+2665: BLACK HEART SUIT)

Any character can appear between the "double quotes", except the quote itself or newline character.

Literal newline is not allowed. The following is syntax error.

var x = "can't do this"

To include newline, use \n.

To include a quote character, use \", e.g. "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 is a Sequence of Bytes

Golang string is a sequence of bytes, not characters.

Normally, we use string as a sequence of characters. Go string can contain any Unicode character.

In go, each character is stored as 1 to 4 bytes by utf8 encoding.

if you are not familiar with unicode, encoding, first read Unicode Basics: Character Set, Encoding, UTF-8

You can use string to store bytes, any byte.

String can contain byte sequences that is not valid encoding of any Unicode character.

You can create a string of any byte by using the hexadecimal escape \xhh

[see Golang: String Backslash Escape]

For example, character A has codepoint 65 in decimal , and 41 in hexadecimal. So, "A" and "\x41" creates the same string. But you can create byte sequences that's not valid encoding of any Unicode character.

package main

import "fmt"

func main() {

	fmt.Printf("%v\n", "A" == "\x41") // true

}

String Index

s[n] → returns the nth byte of string s. The return value's type is unit8. (unit8 is an alias of byte)

[see Golang: Basic Types]

Index start at 0.

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 Functions

Golang: String Functions

Print String: Bytes vs Characters

Golang: Print String

Rune

Before you can work with string as character (instead of byte) sequence, you need to understand rune.

Golang: Rune

Working with String as Character Sequence

Golang: String, Byte Slice, Rune Slice

Reference

The Go Programming Language Specification - The Go Programming Language#String_literals

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. String Functions
  8. Print String
  9. String Backslash Escape
  10. Rune
  11. Variable
  12. Zero Value
  13. Constant
  14. If Then Else
  15. Switch/Case
  16. Loop
  17. Basic Types
  18. Array
  19. Slice
  20. Map
  21. Struct
  22. Function
  23. Closure
  24. Pointer
  25. String, Byte Slice, Rune Slice
  26. regexp
  27. Read File
  28. Write to File
  29. Walk Dir
  30. Check File Exist
  31. System Call
  32. Get Script Path
  33. Defer
  34. Random Number

Examples

  1. Match Any Regexp
  2. Find Replace
  3. Validate Links
  4. Generate Sitemap

Reference

  1. Go Spec