python中的chr和ord函数_python的ord(),chr()在等同于go?

1586010002-jmsa.png

What is the equivalent of python's chr() and ord() functions in golang?

chr(97) = 'a'

ord('a') = 97

解决方案

They are supported as simple conversions:

ch := rune(97)

n := int('a')

fmt.Printf("char: %c\n", ch)

fmt.Printf("code: %d\n", n)

Output (try it on the Go Playground):

char: a

code: 97

Note: you can also convert an integer numeric value to a string which basically interprets the integer value as the UTF-8 encoded value:

s := string(97)

fmt.Printf("text: %s\n", s) // Output: text: a

Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".

你可能感兴趣的:(python中的chr和ord函数_python的ord(),chr()在等同于go?)