Itoa Versus String Of Rune

strconv.Itoa renders decimal digits while string(n) treats the integer as a Unicode code point, so the two are rarely interchangeable.

GoData Integrity

Code

gosnippet
21 lines
package main

import (
	"fmt"
	"strconv"
)

func main() {
	// strconv.Itoa renders decimal digits versus string(n), which treats the integer as a rune code point
	fmt.Println("digits:", strconv.Itoa(65), strconv.Itoa(300))
	fmt.Println("code points:", string(65), string(300))

	// code points are how unicode lands in strings
	fmt.Println("emoji:", string(rune(0x1F600)))

	// negative or out-of-range values collapse to the replacement rune
	fmt.Println("negative rune:", string(rune(-1)))

	// the vet warning exists because string(65) is almost never what you meant
	fmt.Println("round trip:", strconv.Itoa('A'), string(rune('A')))
}

What drives the file

Itoa Versus String Of Rune keeps everything in the function main.

Using it

The demo output is labeled (digits:, code points:, emoji:); each label names the call that printed it.

Key statements

package main

Interactive Tools for This Topic

FAQ

What does Itoa Versus String Of Rune print when run?
Invoke the file as-is — the `digits:`, `code points:`, `emoji:` prefixes identify which step printed each line.