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

}

Print One Byte in String

When you use a string index, e.g. "abc"[0], the value is a byte.

You may print it as hexadecimal, or integer (unicode codepoint), or as a char.

Example:

package main

import "fmt"

// ways to print 1 byte in string

func main() {

	var x = "abc"

	fmt.Printf("%#v\n", x[0]) // 0x61
	fmt.Printf("%d\n", x[0])  // 97
	fmt.Printf("%q\n", x[0])  // 'a'

}

Note, a index in string may not be a valid character.

package main

import "fmt"

// a index in string may not be a valid character

func main() {

	// utf8 encoding for FACE WITH TEARS OF JOY char is 4 bytes: F0 9F 98 82
	var y = "😂"

	// print 1st byte
	fmt.Printf("%#v\n", y[0]) // 0xf0
	fmt.Printf("%d\n", y[0])  // 240
	fmt.Printf("%q\n", y[0])  // 'ð'

}

SubString

s[n:m] → returns a substring of s from index n to m (excluding m). The return value's type is string.

package main

import "fmt"

func main() {

	var x = "012345"

	// substring
	fmt.Printf("%#v\n", x[2:3]) // "2"

	fmt.Printf("%#v\n", x[2:4]) // "23"

	fmt.Printf("%#v\n", x[2:2]) // ""

}

Remember, string is a sequence of bytes. So, if you have non-ASCII unicode in string, arbitrary index range may create a string that's not a sequence of characters.

package main

import "fmt"

func main() {

	const x = "♥♥♥"

	fmt.Printf("%#v\n", x[2:4]) // "\xa5\xe2"

}

[see Golang: String, Byte Slice, Rune Slice]

[see ASCII Characters ␀ ␣ ¶]

Length (Number of Bytes)

len(string) → returns the number of bytes in string.

package main

import "fmt"

func main() {
	fmt.Printf("%v\n", len("abc")) // 3
	fmt.Printf("%v\n", len("ab♥")) // 5
}

Join String

Use + to join string. e.g.

"abc" + "def"

package main

import "fmt"

func main() {
	fmt.Printf("%v\n", "a"+"b") // ab
}

Embed Expression in String?

There is no embeding expression in string. (as in Ruby or JavaScript)

The closest is this:

fmt.Sprintf("Name: %v\nAge: %v", "John", 10)

package main

import "fmt"

func main() {

	var name = "John"
	var age = 30

	var x = fmt.Sprintf("Name: %v, Age: %v", name, age)

	fmt.Println(x) // Name: John, Age: 30
}

String Functions

String functions are in package “strings”.

see https://golang.org/pkg/strings/

Here's some example.

package main

import "fmt"
import "strings"

var pl = fmt.Println

func main() {

	pl("ab" == "ab") // 0

	pl(strings.Contains("abcd", "bc"))  // true
	pl(strings.HasPrefix("abca", "ab")) // true
	pl(strings.HasSuffix("abca", "ca")) // true

	pl(strings.ToLower("ABC") == "abc") // true

	pl(strings.Trim(" abc ", " ") == "abc") // true

	pl(strings.Count("abcaab", "ab") == 2) // true

	pl(strings.Index("abc", "bc") == 1) // true

	pl(strings.Join([]string{"a", "and", "b"}, " ") == "a and b") // true

	// split into slice
	pl(strings.Split("a b c", " ")) // [a b c]
}

Find Replace

Use the regex package.

See: Golang: regexp

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. Print String
  8. String Backslash Escape
  9. Rune
  10. Variable
  11. Zero Value
  12. Constant
  13. If Then Else
  14. Switch/Case
  15. Loop
  16. Basic Types
  17. Array
  18. Slice
  19. Map
  20. Struct
  21. Function
  22. Closure
  23. Pointer
  24. String, Byte Slice, Rune Slice
  25. regexp
  26. Read File
  27. Write to File
  28. Walk Dir
  29. Check File Exist
  30. System Call
  31. Get Script Path
  32. Defer
  33. Random Number

Examples

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

Reference

  1. Go Spec