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 = `Alice was beginning to get very tired of sitting by her
sister on the bank, and of having nothing to do: once or twice she had
peeped into the book her sister was reading, but it had no pictures or
conversations in it, «and what is the use of a book,» thought Alice «without
pictures or conversation?».`

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

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.

[see Golang: String Backslash Escape]

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

use fmt.Printf() to print byte.

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'

	var y = "👍"
	// utf8 encoding for thumb up char emoji 👍 is 4 bytes: #xF0 #x9F #x91 #x8D

	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 as Chars]

[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

Because string is byte sequence, sometimes you want to print them as hexadecimal to see the bytes. Other times you want to print them as characters.

The fmt.Printf function has several verbs to help.

[see ASCII Table]

package main

import "fmt"

func main() {
    var x = "♥\t😂" // with a tab (U+0009) in middle

    fmt.Printf("%s\n", x)  // ♥ 😂
    fmt.Printf("%q\n", x)  // "♥\t😂"
    fmt.Printf("%+q\n", x) // "\u2665\t\U0001f602"
    fmt.Printf("% x\n", x) // e2 99 a5 09 f0 9f 98 82

    // turn the string into rune slice, then print it with %U
    fmt.Printf("%U\n", []rune(x)) // [U+2665 U+0009 U+1F602]
}

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 as Chars

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

Examples

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

Reference

  1. Go Spec