0% found this document useful (0 votes)
7 views2 pages

Go by Example - Strings and Runes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Go by Example - Strings and Runes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Go by Example: Strings and Runes https://gobyexample.

com/strings-and-runes

Go by Example: Strings and Runes


A Go string is a read-only slice of bytes. The
language and the standard library treat strings
specially - as containers of text encoded in UTF-8.
In other languages, strings are made of
“characters”. In Go, the concept of a character is
called a rune - it’s an integer that represents a
Unicode code point. This Go blog post is a good
introduction to the topic.

package main

import (
"fmt"
"unicode/utf8"
)

func main() {

s is a string assigned a literal value representing const s = "สวั สดี"


the word “hello” in the Thai language. Go string
literals are UTF-8 encoded text.

Since strings are equivalent to []byte, this will fmt.Println("Len:", len(s))


produce the length of the raw bytes stored within.

Indexing into a string produces the raw byte for i := 0; i < len(s); i++ {
values at each index. This loop generates the hex fmt.Printf("%x ", s[i])
values of all the bytes that constitute the code }
fmt.Println()
points in s.

To count how many runes are in a string, we can fmt.Println("Rune count:", utf8.RuneCountInString(s))
use the utf8 package. Note that the run-time of
RuneCountInString depends on the size of the
string, because it has to decode each UTF-8 rune
sequentially. Some Thai characters are
represented by UTF-8 code points that can span
multiple bytes, so the result of this count may be
surprising.

A range loop handles strings specially and decodes for idx, runeValue := range s {
each rune along with its offset in the string. fmt.Printf("%#U starts at %d\n", runeValue, idx)
}

We can achieve the same iteration by using the fmt.Println("\nUsing DecodeRuneInString")


utf8.DecodeRuneInString function explicitly. for i, w := 0, 0; i < len(s); i += w {
runeValue, width := utf8.DecodeRuneInString(s[i:])
fmt.Printf("%#U starts at %d\n", runeValue, i)
w = width

This demonstrates passing a rune value to a examineRune(runeValue)


function. }
}

func examineRune(r rune) {

Values enclosed in single quotes are rune literals. if r == 't' {


We can compare a rune value to a rune literal fmt.Println("found tee")
directly. } else if r == 'ส' {
fmt.Println("found so sua")
}
}

$ go run strings-and-runes.go
Len: 18
e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5
Rune count: 6

1 of 2 11/26/24, 23:34
Go by Example: Strings and Runes https://gobyexample.com/strings-and-runes

U+0E2A 'ส' starts at 0


U+0E27 'ว' starts at 3
U+0E31 'ั' starts at 6
U+0E2A 'ส' starts at 9
U+0E14 'ด' starts at 12
U+0E35 'ี' starts at 15

Using DecodeRuneInString
U+0E2A 'ส' starts at 0
found so sua
U+0E27 'ว' starts at 3
U+0E31 'ั' starts at 6
U+0E2A 'ส' starts at 9
found so sua
U+0E14 'ด' starts at 12
U+0E35 'ี' starts at 15

Next example: Structs.

by Mark McGranaghan and Eli Bendersky | source | license

2 of 2 11/26/24, 23:34

You might also like