A Guide to Types and Casting in Golang

In this blog, we’ll be looking at the different data types that are used in Golang and how to cast between different kinds. Before we look at different ways of type casting, let’s discuss some of the most commonly used data types in Golang.

Data types in Golang
Here’s a list of data types that you’ll use in Golang:

    • •The bool data type. It’s used to store a set of Boolean truth values. It can either be true or false.


    • •The string data type. A Golang string is implemented as a set of bytes that store a sequence of characters, using a given character encoding scheme.


    • •The int data type. A Golang int data type stores whole numbers (integers) of varying sizes. They include the int8 for 8 bits, the int16 for 16 bits, the int32 for 32 bit and the int64 for 64 bits.


    • •The uint data types. It represents the unsigned whole numbers (integer types of varying lengths. For instance, the uint8 for 8 bit, the uint16 for 16 bits, the uint32 for 32 bits and the uint64 for 64 bits.


    • •The byte. It stores integer values for 8 bits. It’s the same as the uint8.


    • •The float32 and the float64. They are used for storing floating point numbers for 32 bits and 64 bits respectively.


    •The complex64 and the complex128 data types. It’s used to represent complex numbers for 64 bits and 128 bits respectively.

What about type casting?
Well, type casting allows programmers to change an entity from one data type into another. You may ask, “Why do I need type casting in Golang?” Well, if you need to take advantage of certain characteristics of data type hierarchies, then you have to change entities from one data type into another.

For instance, if you have a byte—or uint8—and you would like this to be converted into larger representation for arithmetic calculations, then you should cast such data types. So, are you ready to begin type casting in Golang?

Let’s dive in.

In Golang, we use the expression T (v) to convert the value v to the data type T. The example below illustrates how you can convert from one data type to another for numeric data:

var amount int = 80
var amount2 float64 = float64 (amount)
var amount3 uint = uint (f)

This can be summarized as follows:

amount: = 80
amount2:= float64 (amount)
amount3:= uint (f)

Here’s is what you should note about type casting in Golang: if you use the Go assignment between the items of different data types, you must have an explicit conversion. That’s why it’s important to it’s vital that you differentiate between using the “:=” or the “var= expression” when type casting in Golang.

For instance, when to declare a variable without specifying as an explicit data type—by using the “:= syntax” or the “var = expression syntax”—the variable’s data type will be inferred from the value that appears on the right hand side. If the right hand side of your declaration is typed, the new variable will be of the same data type. Here’s an example:

var amount int
amount1:= amount // In this case, amount is of int data type

Now what happens when the right hand side is un-typed? Well, when the right hand side has an un-typed numeric constant, then new variable—in this case amount1—may assume an int, float64, or even complex128 depending on the precision of your constant.

The example below illustrates this:

i: = 42          		 // This is an int data type depending on the accuracy. 
pi: = 3.142		// This is a float64 data type depending on the precision. 
b := 0.867 + 0.5i 	// This is a complex128 depending on the precision. 


(Visited 15,643 times, 1 visits today)