Converting Numeric Types
var x = 3 // int type
var y = 3.1 // float64 type
x = x * y // mismatched types
x = x * int(y) // works
var x = 3
var y int64 = 2
x = y // cannot assign int64 to int
Converting Numbers to String and vice versa
s := string(99) // converts ascii c char
// go vet fails
s := fmt.Sprintf("%d", 99) // use fmt.Sprintf
s3 := strconv.Itoa(20)
s1 := string(4.3) // cannot convert float to string
s1 := fmt.Sprintf("%f", 4.3)
string to numbers
s1 := "7.1"
f1, _ := strconv.ParseFloat(s1, 64) // bitsize 64 returns float64
i, err := strconv.Atoi("-2")
Top comments (0)