Ben Chuanlong Du's Blog

It is never too late to learn.

Strings in Golang Are UTF-8

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Unicode

In [1]:
len("尼")
Out[1]:
3
In [1]:
s := "在线中文输入"
In [2]:
s
Out[2]:
在线中文输入
In [3]:
len(s)
Out[3]:
18
In [4]:
import "unicode/utf8"
In [5]:
q := "A to Z"
q
Out[5]:
A to Z
In [7]:
fc, size1 := utf8.DecodeRuneInString(q)
In [8]:
fc
Out[8]:
65
In [9]:
size1
Out[9]:
1
In [10]:
lc, size2 := utf8.DecodeLastRuneInString(q)
In [11]:
lc
Out[11]:
90
In [12]:
size2
Out[12]:
1
In [14]:
fc, size1 := utf8.DecodeRuneInString(s)
In [15]:
fc
Out[15]:
22312
In [16]:
size1
Out[16]:
3
In [ ]:
 

Comments