[Golang] Get UTF-8 String Width


Get UTF-8 string width (width of English letter is 1, width of CJK character is 2) in Golang.

Install package width:

$ go get -u golang.org/x/text/width

Source code:

width.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package mylib

import "golang.org/x/text/width"

func GetWidthUTF8String(s string) int {
	size := 0
	for _, runeValue := range s {
		p := width.LookupRune(runeValue)
		if p.Kind() == width.EastAsianWide {
			size += 2
			continue
		}
		if p.Kind() == width.EastAsianNarrow {
			size += 1
			continue
		}
		panic("cannot determine!")
	}
	return size
}
width_test.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package mylib

import "testing"

func TestGetWidthUTF8String(t *testing.T) {
	if GetWidthUTF8String("s: 你好") != 7 {
		t.Error("s: 你好")
	}
	if GetWidthUTF8String("s --asji勿勿") != 12 {
		t.Error("s --asji勿勿")
	}
}

Tested on: Ubuntu Linux 15.10, Go 1.6.


References:

[1][Golang] Iterate Over UTF-8 Strings (non-ASCII strings)
[2]
[3]python get character width