Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go unicode code points

Tags:

unicode

go

How do you encode unicode code points in Go? In the example below I'm storing the hex representation of the unicode for ace of spades as \u1F0A1 but when I print it comes out as Ἂ1. Why is that? If I copy and paste the ace of spades glyph it prints fine.

package main

import "fmt"

func main() {
    fmt.Println("🂡 \u1F0A1")
}

Output

🂡 Ἂ1

Example above in the Go playground https://play.golang.org/p/ukK57CnVuE

like image 300
Neil Avatar asked Mar 17 '23 01:03

Neil


1 Answers

Lowercase \u is for Unicode code points from \u0000 to \uFFFF. Use \U if you want to have code points above 0xFFFF:

package main

import "fmt"

func main() {
    fmt.Println("🂡 = \U0001F0A1")
}

See also: playground and the string literals section of the specification.

like image 73
Ainar-G Avatar answered Mar 27 '23 20:03

Ainar-G