Essential Go Basic types  Suggest an edit

Integers

Go has fixed-size signed and unsigned integers:

It also has architecture-dependent integers:

Zero value of an integer is 0.

Convert int to string with strconv.Itoa

var i1 int = -38
fmt.Printf("i1: %s\n", strconv.Itoa(i1))

var i2 int32 = 148
fmt.Printf("i2: %s\n", strconv.Itoa(int(i2)))
i1: -38
i2: 148

Convert int to string with fmt.Sprintf

var i1 int = -38
s1 := fmt.Sprintf("%d", i1)
fmt.Printf("i1: %s\n", s1)

var i2 int32 = 148
s2 := fmt.Sprintf("%d", i2)
fmt.Printf("i2: %s\n", s2)
i1: -38
i2: 148

Convert string to int with strconv.Atoi

s := "-48"
i1, err := strconv.Atoi(s)
if err != nil {
	log.Fatalf("strconv.Atoi() failed with %s\n", err)
}
fmt.Printf("i1: %d\n", i1)
i1: -48

Convert string to int with fmt.Sscanf

s := "348"
var i int
_, err := fmt.Sscanf(s, "%d", &i)
if err != nil {
	log.Fatalf("fmt.Sscanf failed with '%s'\n", err)
}
fmt.Printf("i1: %d\n", i)
i1: 348

  ↑ ↓ to navigate     ↵ to select     Esc to close