Golang : Convert a rune to unicode style string \u
Continuing from previous tutorial on how to get escape characters or \u unicode style string. These are some additional methods to convert rune to unicode style string.
package main
import (
"fmt"
)
func main() {
// use %x for single rune
fmt.Printf("\\u%x\n", 'お')
// fmt.Printf("\\u%x\n", 'おはよう') -- will not work
// use %+q for multiple runes
fmt.Printf("%+q\n", "おはよう")
}
output :
\u304a
"\u304a\u306f\u3088\u3046"
Another way :
package main
import (
"fmt"
)
func main() {
u := fmt.Sprintf("%U", 'お')
fmt.Println(u)
}
Output :
U+304A
or use strconv.QuoteRuneToASCII()
function.
package main
import (
"fmt"
"strconv"
)
func main() {
// translate character to rune
r := rune('お')
// extra the unicode \uxxxx value
unicode := strconv.QuoteRuneToASCII(r)
fmt.Println(string(r)+" unicode string value is : ", unicode)
}
Output :
お unicode string value is : '\u304a'
Reference :
See also : Golang : Get escape characters \u form from unicode characters
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.8k Golang : Google Drive API upload and rename example
+6.6k Golang : Muxing with Martini example
+4.6k Fix Google Analytics Redundant Hostnames problem
+7.6k Golang : Get today's weekday name and calculate target day distance example
+7.5k Gogland : Where to put source code files in package directory for rookie
+5.6k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+6.9k Golang : Array mapping with Interface
+5.4k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+8.1k Golang : Count leading or ending zeros(any item of interest) example
+4.8k Golang : PGX CopyFrom to insert rows into Postgres database
+8.6k Golang : HTTP Routing with Goji example
+6.8k Golang : constant 20013 overflows byte error message