Skip to content

Instantly share code, notes, and snippets.

@copyninja
Created August 13, 2013 08:29
Show Gist options
  • Save copyninja/6219041 to your computer and use it in GitHub Desktop.
Save copyninja/6219041 to your computer and use it in GitHub Desktop.
-*- mode: compilation; default-directory: "c:/Users/invakam2/Documents/Workspace/Go/src/" -*-
Compilation started at Tue Aug 13 13:57:59
go run test_strings.go
Len of normalString 21
len of backquotedString 21
Normal string with range operator yields following:
Unicode value: cb5, rendered value: ವ
Unicode value: cbe, rendered value: ಾ
Unicode value: cb8, rendered value: ಸ
Unicode value: cc1, rendered value: ು
Unicode value: ca6, rendered value: ದ
Unicode value: cc7, rendered value: ೇ
Unicode value: cb5, rendered value: ವ
Backquoted string with range operator yields following:
Unicode value: cb5, rendered value: ವ
Unicode value: cbe, rendered value: ಾ
Unicode value: cb8, rendered value: ಸ
Unicode value: cc1, rendered value: ು
Unicode value: ca6, rendered value: ದ
Unicode value: cc7, rendered value: ೇ
Unicode value: cb5, rendered value: ವ
Indexing operation on normalString: normalString[0]
unicode value: e0 rendered value: à
Indexing operation on backquotedString: backquotedString[0]
unicode value: e0 rendered value à
len(ವ) 3
Compilation finished at Tue Aug 13 13:58:00
@copyninja
Copy link
Author

As seen in the output Go indeed considers string literal as utf-8 encoded stream of chars but trying to index a string will not give you expected behaviour in this case I needed ವ at 0 but got \xe0 which is first byte of utf-8 encoded \u0cb5 which is \xe0 \xb2 \xb5.

Language does allow us directly write any language as string in Go but it doesn't understand Unicode code points natively, but strings library does interpret the unicode properly as well as range key word on string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment