How do I convert full width characters into ascii characters in golang. The input in my program is full width numbers and I need to run some computations on them, so I assume I have to write a convert function like below, Before I start mapping bytes and such I was wondering if this is indeed available in the standard go library
fullWidth:="123"
expected := "123"
func convert(input string) string {
// body
}
expected == convert(fullWidth)
You can use the width.Transformer of the golang.org/x/text package to do the transformation but the standard library does not have this functionality. x/text is one of many official sub-repositories which have weaker compatibility requirements (see here).
Example:
package main
import (
"fmt"
"golang.org/x/text/width"
)
func main() {
s := "123"
n := width.Narrow.String(s)
fmt.Printf("%U: %s\n", []rune(s), s)
fmt.Printf("%U: %s\n", []rune(n), n)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With