How To Convert Data Types in Go

Written by Gopher Guides

In Go, data types are used to classify one particular type of data, determining the values that you can assign to the type and the operations you can perform on it. When programming, there are times when you will need to convert values between types in order to manipulate values in a different way. For example, you may need to concatenate numeric values with strings, or represent decimal places in numbers that were initialized as integer values. User-generated data is often automatically assigned the string data type, even if it consists of numbers; in order to perform mathematical operations in this input, you would have to convert the string to a numeric data type.

Since Go is a statically typed language, data types are bound to variables rather than values. This means that, if you define a variable as an int, it can only be an int; you can’t assign a string to it without converting the data type of the variable. The static nature of data types in Go places even more importance on learning the ways to convert them.

This tutorial will guide you through converting numbers and strings, as well as provide examples to help familiarize yourself with different use cases.

Converting Number Types

Go has several numeric types to choose from. Primarily they break out into two general types: integers and floating-point numbers.

There are many situations in which you may want to convert between numeric types. Converting between different sizes of numeric types can help optimize performance for specific kinds of system architecture. If you have an integer from another part of your code and want to do division on it, you may want to convert the integer to a float to preserve the precision of the operation. Additionally, working with time durations usually involves integer conversion. To address these situations, Go has built-in type conversions for most numeric types.

Converting Between Integer Types

Go has many integer data types to pick from. When to use one over the other is typically more about performance; however, there will be times when you will need to convert from one integer type to another. For example, Go sometimes automatically generates numeric values as int, which may not match your input value. If your input value were int64, you would not be able to use the int and the int64 numbers in the same mathematical expression until you converted their data types to match.

Assume that you have an int8 and you need to convert it to an int32. You can do this by wrapping it in the int32() type conversion:

  1. var index int8 = 15
  2. var bigIndex int32
  3. bigIndex = int32(index)
  4. fmt.Println(bigIndex)

Output

  1. 15

This code block defines index as an int8 data type and bigIndex as an int32 data type. To store the value of index in bigIndex, it converts the data type to an int32. This is done by wrapping the int32() conversion around the index variable.

To verify your data types, you could use the fmt.Printf statement and the %T verb with the following syntax:

  1. fmt.Printf("index data type: %T\n", index)
  2. fmt.Printf("bigIndex data type: %T\n", bigIndex)

Output

  1. index data type: int8
  2. bigIndex data type: int32

Since this uses the %T verb, the print statement outputs the type for the variable, and not the actual value of the variable. This way, you can confirm the converted data type.

You can also convert from a larger bit-size integer to a smaller bit-size integer:

  1. var big int64 = 64
  2. var little int8
  3. little = int8(big)
  4. fmt.Println(little)

Output

  1. 64

Keep in mind that when converting integers you could potentially exceed the maximum value of the data type and wraparound:

  1. var big int64 = 129
  2. var little = int8(big)
  3. fmt.Println(little)

Output

  1. -127

A wraparound happens when the value is converted to a data type that is too small to hold it. In the preceding example, the 8-bit data type int8 did not have enough space to hold the 64-bit variable big. Care should always be taken when converting from a larger number data type to a smaller number data type so that you do not truncate the data by accident.

Converting Integers to Floats

Converting integers to floats in Go is similar to converting one integer type to another. You can use the built-in type conversions by wrapping float64() or float32() around the integer you are converting:

  1. var x int64 = 57
  2. var y float64 = float64(x)
  3. fmt.Printf("%.2f\n", y)

Output

  1. 57.00

This code declares a variable x of type int64 and initializes its value to 57.

  1. var x int64 = 57

Wrapping the float64() conversion around x will convert the value of 57 to a float value of 57.00.

  1. var y float64 = float64(x)

The %.2f print verb tells fmt.Printf to format the float with two decimals.

You can also use this process on a variable. The following code declares f as equal to 57, and then prints out the new float:

  1. var f float64 = 57
  2. fmt.Printf("%.2f\n", f)

Output

  1. 57.00

By using either float32() or float64(), you can convert integers to floats. Next, you will learn how to convert floats to integers.

Converting Floats to Integers

Go can convert floats to integers, but the program will lose the precision of the float.

Wrapping floats in int(), or one of its architecture-independent data types, works similarly to when you used it to convert from one integer type to another. You can add a floating-point number inside of the parentheses to convert it to an integer:

  1. var f float64 = 390.8
  2. var i int = int(f)
  3. fmt.Printf("f = %.2f\n", f)
  4. fmt.Printf("i = %d\n", i)

Output

  1. f = 390.80
  2. i = 390

This syntax would convert the float 390.8 to the integer 390, dropping the decimal place.

You can also use this with variables. The following code declares b as equal to 125.0 and c as equal to 390.8, then prints them out as integers. Short variable declaration (:=) shortens up the syntax:

  1. b := 125.0
  2. c := 390.8
  3. fmt.Println(int(b))
  4. fmt.Println(int(c))

Output

  1. 125
  2. 390

When converting floats to integers with the int() type, Go cuts off the decimal and remaining numbers of a float to create an integer. Note that, even though you may want to round 390.8 up to 391, Go will not do this through the int() type. Instead, it will drop the decimal.

Numbers Converted Through Division

When dividing integer types in Go the result will also be an integer type, with the modulus, or remainder, dropped:

  1. a := 5 / 2
  2. fmt.Println(a)

Output

  1. 2

If, when dividing, any of the number types are a float, then all of the types will automatically be declared as a float:

  1. a := 5.0 / 2
  2. fmt.Println(a)

Output

  1. 2.5

This divides the float 5.0 by the integer 2, and the answer 2.5 is a float that retains the decimal precision.

In this section, you have converted between different number data types, including differing sizes of integers and floating-point numbers. Next, you will learn how to convert between numbers and strings.

Converting with Strings

A string is a sequence of one or more characters (letters, numbers, or symbols). Strings are a common form of data in computer programs, and you may need to convert strings to numbers or numbers to strings fairly often, especially when you are taking in user-generated data.

Converting Numbers to Strings

You can convert numbers to strings by using the strconv.Itoa method from the strconv package in the Go standard libary. If you pass either a number or a variable into the parentheses of the method, that numeric value will be converted into a string value.

First, let’s look at converting integers. To convert the integer 12 to a string value, you can pass 12 into the strconv.Itoa method:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. a := strconv.Itoa(12)
  8. fmt.Printf("%q\n", a)
  9. }

When running this program, you’ll receive the following output:

Output

  1. "12"

The quotes around the number 12 signify that the number is no longer an integer but is now a string value.

You used the := assignment operator to both declare a new variable with the name of a and assign the value returned from the strconv.Itoa() function. In this case, you assigned the value 12 to your variable. You also used the %q verb in the fmt.Printf function, which tells the function to quote the string provided.

With variables you can begin to see how practical it can be to convert integers to strings. Say you want to keep track of a user’s daily programming progress and are inputting how many lines of code they write at a time. You would like to show this feedback to the user and will be printing out string and integer values at the same time:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. user := "Sammy"
  7. lines := 50
  8. fmt.Println("Congratulations, " + user + "! You just wrote " + lines + " lines of code.")
  9. }

When you run this code, you’ll receive the following error:

Output

  1. invalid operation: ("Congratulations, " + user + "! You just wrote ") + lines (mismatched types string and int)

You’re not able to concatenate strings and integers in Go, so you’ll have to convert the variable lines to be a string value:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. user := "Sammy"
  8. lines := 50
  9. fmt.Println("Congratulations, " + user + "! You just wrote " + strconv.Itoa(lines) + " lines of code.")
  10. }

Now, when you run the code, you’ll receive the following output that congratulates your user on their progress:

Output

  1. Congratulations, Sammy! You just wrote 50 lines of code.

If you are looking to convert a float to a string rather than an integer to a string, you follow similar steps and format. When you pass a float into the fmt.Sprint method, from the fmt package in the Go standard library, a string value of the float will be returned. You can use either the float value itself or a variable:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. fmt.Println(fmt.Sprint(421.034))
  7. f := 5524.53
  8. fmt.Println(fmt.Sprint(f))
  9. }

Output

  1. 421.034
  2. 5524.53

You can test to make sure it’s right by concatenating with a string:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. f := 5524.53
  7. fmt.Println("Sammy has " + fmt.Sprint(f) + " points.")
  8. }

Output

  1. Sammy has 5524.53 points.

You can be sure your float was properly converted to a string because the concatenation was performed without error.

Converting Strings to Numbers

Strings can be converted to numbers by using the strconv package in the Go standard library. The strconv package has functions for converting both integer and float number types. This is a very common operation when accepting input from the user. For example, if you had a program that asked for a person’s age, when they type the response in, it is captured as a string. You would then need to convert it to an int to do any math with it.

If your string does not have decimal places, you’ll most likely want to convert it to an integer by using the strconv.Atoi function. If you know you will use the number as a float, you would use strconv.ParseFloat.

Let’s use the example of the user Sammy keeping track of lines of code written each day. You may want to manipulate those values with math to provide more interesting feedback for the user, but those values are currently stored in strings:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. lines_yesterday := "50"
  7. lines_today := "108"
  8. lines_more := lines_today - lines_yesterday
  9. fmt.Println(lines_more)
  10. }

Output

  1. invalid operation: lines_today - lines_yesterday (operator - not defined on string)

Because the two numeric values were stored in strings, you received an error. The operand - for subtraction is not a valid operand for two string values.

Modify the code to include the strconv.Atoi() method that will convert the strings to integers, which will allow you to do math with values that were originally strings. Because there is a potential to fail when converting a string to an integer, you have to check for any errors. You can use an if statement to check if your conversion was successful.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "strconv"
  6. )
  7. func main() {
  8. lines_yesterday := "50"
  9. lines_today := "108"
  10. yesterday, err := strconv.Atoi(lines_yesterday)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. today, err := strconv.Atoi(lines_today)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. lines_more := today - yesterday
  19. fmt.Println(lines_more)
  20. }

Because it is possible for a string to not be a number, the strconv.Atoi() method will return both the converted type, as well as a potential error. When converting from lines_yesterday with the strconv.Atoi function, you have to check the err return value to ensure that the value was converted. If the err is not nil, it means that strconv.Atoi was unable to successfully convert the string value to an integer. In this example, you used an if statement to check for the error, and if an error was returned, you used log.Fatal to log the error and exit the program.

When you run the preceding code, you will get:

Output

  1. 58

Now try to convert a string that is not a number:

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. func main() {
  7. a := "not a number"
  8. b, err := strconv.Atoi(a)
  9. fmt.Println(b)
  10. fmt.Println(err)
  11. }

You will get the following error:

Output

  1. 0
  2. strconv.Atoi: parsing "not a number": invalid syntax

Because b was declared, but strconv.Atoi failed to make a conversion, a value was never assigned to b. Notice that b has the value of 0. This is because Go has default values, referred to as zero values in Go. strconv.Atoi provides an error describing why it failed to convert the string as well.

Converting Strings and Bytes

Strings in Go are stored as a slice of bytes. In Go, you can convert between a slice of bytes and a string by wrapping it in the corresponding conversions of []byte() and string():

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. a := "my string"
  7. b := []byte(a)
  8. c := string(b)
  9. fmt.Println(a)
  10. fmt.Println(b)
  11. fmt.Println(c)
  12. }

Here you have stored a string value in a, then converted it to a slice of bytes b, then converted the slice of bytes back to a string as c. You then print a, b, and c to the screen:

Output

  1. my string
  2. [109 121 32 115 116 114 105 110 103]
  3. my string

The first line of output is the original string my string. The second line printed out is the byte slice that makes up the original string. The third line shows that the byte slice can be safely converted back into a string and printed back out.

Conclusion

This Go tutorial demonstrated how to convert several of the important native data types to other data types, primarily through built-in methods. Being able to convert data types in Go will allow you to do things like accept user input and do math across different number types. Later on, when you are using Go to write programs that accept data from many different sources like databases and APIs, you will use these conversion methods to ensure you can act on your data. You will also be able to optimize storage by converting data to smaller data types.

If you would like a deeper analysis of data types in Go, check out our Understanding Data Types in Go article.