I have Urdu text and I want to encode it in Go.
A user name is like this
username := `ابو ناصر الحرسانی`
I read about the encoding package of Go but I'm not able to get my desired result. I want to read all the bytes and a string equivalent encoded value of this variable.
Go uses UTF-8 encoding for the string
type. Use string
to convert a slice of UTF-8 encoded bytes ([]byte
) to a UTF-8 encoded string
. A string
is a sequence of byte
s; it is an immutable copy of the byte
slice.
package main
import "fmt"
func main() {
username := `ابو ناصر الحرسانی`
userbytes := []byte(username)
fmt.Printf("%[1]T: %[1]v
", userbytes)
userstring := string(userbytes)
fmt.Printf("%[1]T: %[1]v
", userstring)
}
Playground: https://play.golang.org/p/Ugwj-CxmQw0
Output:
[]uint8: [216 167 216 168 217 136 32 217 134 216 167 216 181 216 177 32 216 167 217 132 216 173 216 177 216 179 216 167 217 134 219 140]
string: ابو ناصر الحرسانی
Type byte
is an alias of type uint8
.
The Go Blog: Strings, bytes, runes and characters in Go
The Go Programming Language Specification
Wikipedia: UTF-8
Unicode: UTF-8, UTF-16, UTF-32 & BOM