Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Converting values to other types - float32, int, string

  • float32()

  • int()

  • string()

  • Sprintf

  • %f

  • %d

  • integers to float32()

  • floats to int()

  • integers to string() but that converts the number to the value it represents in ASCII or Unicode table.

  • In order to get the same "look" but as a string we need to use the Sprintf function from fmt.

package main

import (
	"fmt"
)

func main() {
	n := 65
	q := float32(n)
	fmt.Printf("%v %T\n", n, n) //  65 int
	fmt.Printf("%v %T\n", q, q) // 65 float32

	f := 42.23
	p := int(f)
	fmt.Printf("%v %T\n", f, f) // 42.23 float64
	fmt.Printf("%v %T\n", p, p) // 42 int

	ns := string(n)
	fmt.Printf("%v %T\n", ns, ns) // A, string

	ns2 := fmt.Sprintf("%d", n)
	fmt.Printf("%v %T\n", ns2, ns2) // 65, string

	fs := fmt.Sprintf("%f", f)
	fmt.Printf("%v %T\n", fs, fs) // 42.230000, string

	fmt.Println()
	num := 258
	fmt.Printf("%v %T\n", num, num) // 258 int
	num16 := int16(num)
	fmt.Printf("%v %T\n", num16, num16) // 258 int16
	num8 := int8(num)
	fmt.Printf("%v %T\n", num8, num8) // 2 int8 - loss of the high bytes!
}