Golang strconv, Convert Int to String

Cast numeric types and convert values to strings. Perform map and slice conversions.

Conversion, strconv. Things in Go have types. An int is no float. An int is no string. When required, we can convert between types with built-in casts or more complex custom methods.

In most languages, casting involves a series of complex considerations. It often causes errors. Go here is no exception. Some casts are incompatible.

Cast float, int. This program casts a float constant to an int. We call the int cast like a method. We can assign its result to an int variable.
Here: We have a float of value 1.2. When cast to an int, we are left with 1—the numbers after the decimal place are dropped.
Golang program that casts float to int package main import "fmt" func main() { size := 1.2 fmt.Println(size) // Convert to an int with a cast. i := int(size) fmt.Println(i) } Output 1.2 1

Convert string, ints. We cannot just cast ints to strings (or strings to ints). We must use methods in the strconv module. We can use ParseInt, FormatInt, or the convenient Itoa and Atoi.ParseInt
Tip: For simple cases (like when numbers are base 10, as they usually are) the Itoa and Atoi methods are typically clearer.
Golang program that converts strings, ints with Itoa package main import ( "fmt" "strconv" ) func main() { value := 120 // Use Itoa to convert int to string. result := strconv.Itoa(value) fmt.Println(result) if result == "120" { fmt.Println(true) } // Use Atoi to convert string to int. original, _ := strconv.Atoi(result) if original == 120 { fmt.Println(true) } } Output 120 true true

Slice to string. Here we convert a slice of strings into a single string. We use the strings.Join method. A comma character is inserted between all strings in the output.Convert Slice, String
Golang program that converts slice to string package main import ( "fmt" "strings" ) func main() { // Slice of 3 strings. values := []string{"fish", "chicken", "lettuce"} // Convert slice into a string. result := strings.Join(values, ","); fmt.Println(result) } Output fish,chicken,lettuce

String, rune slice. Characters in a string are called runes—these are like ints. We can convert a string into a slice of runes. We also convert a rune slice into a string.
Tip: The runes here can be manipulated like any other element in a slice. This allows us to modify strings in many ways.
Golang program that converts string to rune slice package main import "fmt" func main() { // An example string. value := "cat" fmt.Println(value) // Convert string into a slice of runes. slice := []rune(value) fmt.Println(slice) // Convert rune slice into a string. original := string(slice) fmt.Println(original) } Output cat [99 97 116] cat

Slice. More complex types (like map) can be converted also. We can use methods like append() to build up slices from the parts of a map.Convert Map, Slice

Summary. Conversions are a recurring problem in programs. With types, we gain reliability. But often, to call methods, we must convert variables into compatible ones.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.