DEV Community

Cover image for Primitive Data Types in Golang
Neel Modi
Neel Modi

Posted on

Primitive Data Types in Golang

Deep Dive into Golang’s Primitive Data Types and How To Use Them.

In the last blog, we discussed how to print text in the console. For that, we used string as the underlying type for our input variable. Now, in this blog, we will work on different types, actually different primitive data types provided by Go. Later, we will cover complex data types.

So, what are primitive data types?

Think of primitive data types as atoms. Atoms are the smallest particles in the universe which cannot be further broken. (Though we can use the latest technology to break them but we will leave it for the physicists.) Like atoms, primitive data types also cannot be further broken down. And like atoms, on combining them, we can form complex data types but more on that later.

Primitive data types in Golang

In golang, we have int, float, byte, string, rune & bool(boolean if you are coming from other programming languages).

1) Int

Int is one of the numeric types which represents a set of integers. There are various kinds of integer types associated with int. We have uint or unsigned integers, complex numbers, and int itself. Along with them are the different sets of numbers that they contain. For example, int8 contains all the 8-bit integers from -128 to +127, int16 contains all the 32-bit integers from -32768 to 32767.

By default, when we write int, the bits associated with it depends on the architecture of your computer. It can be either 32 or 64 bits.

2) Floats

Floats are also numeric types. They represent the decimal numbers. We have some special formatting on the floats which are known as verbs. Verbs are used to set the precision of the floating-point numbers. We will cover verbs when we will work on a problem where we have to calculate the GPA of a student.

In general, verbs are not only associated with floats. Verbs are the different formatting styles that we can apply when using the Printf method from package fmt.

3) String

A string type represents a series of string values or characters if you may. String type is usually used when we want to name things out or to write sentences. There are various needs that strings can satisfy but in layman’s terms, they are used for writing characters as we write words and sentences.

4) Boolean

When the outcome of our input can either be true or false, we use Boolean.

We have discussed the primitive data types, let’s look at them in action.

We always declare the variable in this way. (At least for now)

Var {Keyword} followed by the variable name followed by Data Type.

Problem #1: We want to populate the information of a student.

We would like to have his name, his age, his address, and his GPA for the last sem. Let name = Sam, age = 19, address = Encino, California and GPA = 3.9 {You can populate the data of your own choice.}

First, try it on your own and see if you can do this, I know you will. But here’s my take on it.

Okay, so this is how I would do it.

Note: This exercise is just for the shake of getting started with Go. We will discuss how to write it in a more approachable way in upcoming blogs.Note: This exercise is just for the shake of getting started with Go. We will discuss how to write it in a more approachable way in upcoming blogs.

So that’s it for this blog, in the next blog, we will further optimize our code and learn about Zero Values.

Top comments (0)