Conversion

Contents

Conversion#

Type conversion is explicit. The compiler never converts an int to a string (or to float64) silently.

Numeric conversions are constructor-style.

var i int = 65
var f float64 = float64(i)
var r rune    = rune(i)         // 'A'
var b byte    = byte(i)

Integer to decimal string with strconv.Itoa.

s := strconv.Itoa(42)            // "42"

String to integer with strconv.Atoi; returns an error.

n, err := strconv.Atoi("42")

Parse a typed value with the standard library.

ip := net.ParseIP("1.2.3.4")

string(int) is a Unicode code-point conversion, not a decimal print; go vet warns. Use strconv.Itoa or fmt.Sprintf.

References#

  • Primitives for the numeric types these conversions cross.

  • Strings for byte/rune conversion forms.