W3Basic Logo

Go Data Types

In computer science and computer programming, a data type is a classification of data that tells the compiler or interpreter what set of values a variable or the object can hold.

The common data types used in Go are as follows:

Data TypesDescriptionExamples
intIntegers classified into signed and Unsigned integers1, 20, 100, -100, 2000
floatFloating point/Decimal numbers3.14, 44.5, 50.0, -22.22
boolBoolean values true or false.true, false
stringSequence of characters."Golang", "Welcome to Go"
complexComplex numbers.2+3i, -9.5+18.3i
byteA byte (8 bits) of non-negative integers.107, 99, 8
runeA rune in nothing but a character and rune literals are 32 bit integer values'a', 'B', '[' ,']'

What is a data type?

When writing a computer program, you should not be able to compare apples and oranges or to multiply bananas by tomatoes. Indeed, what could 34 / "hello" even mean?

In computer science terms, you should not be able to perform operations on values of incompatible types: 34 is an integer, "hello" is a string, and you cannot divide an integer by a string.

Note: Some languages, such as JavaScript, try to make sense of all possible operations on all possible types, but it leads to bugs and unexpected behaviors.

Integers

A year, the number of people in a group, and the number of points in a game… are all examples of quantities that can be represented by integers.

In Go, integers can be:

  • signed (positive, negative, or zero) or unsigned (positive or zero),
  • 8, 16, 32, or 64 bits long (a bit is a binary digit, which can be either 0 or 1, 101101 in binary=45 in decimal is an example of a 6-bit number).

Different types of Integers in Golang:

Data typeSize
int/uinteither 32 bits (4 bytes) or 64 bits (8 bytes)
int8/uint88 bits (1 byte)
int16/uint1616 bits (2 bytes)
int32/uint3232 bits (4 bytes)
int64/uint6464 bits ( 8 bytes)

You will rarely use unsigned integers (uint) except when you need to work explicitly with bits and bytes, which is beyond the scope of those first tutorials. Similarly, you will rarely specify the length of an integer (e.g., int32) as the default length (usually 32 bits, which can represent numbers between -2^31 and 2^31-1) is usually enough.

In all cases, the basic operators you can use are the same for all those types:

  • + to add two integers,
  • - to subtract two integers,
  • * to multiply two integers,
  • / to divide two integers (discarding the remainder),
  • % to get the remainder of the division of two integers.

Floating-point numbers

To represent quantities such as the price of a product, the speed of a car, or the distance between two points, integers are not enough: you need to use floating-point numbers (also called floats).

As their name suggests, floats are numbers with a decimal part. For example, 3.14 is a float, 3 is an integer, and 3. is also a float (the decimal part is zero).

In Go, they come in two versions:

  • float32 is a 32-bit float,
  • float64 is a 64-bit float.

Most of the time, you will simply use float64 as it is the default type for floats.

Strings of characters

We have already encountered strings in the previous tutorial (e.g., "Hello, World!"). A string is a sequence of characters, which can be letters, numbers, punctuation marks, or even emojis.

Fortunately, there is only one type of strings in Go: string.

The main operator you can use with strings is +, which concatenates two strings, for example "Hello, " + "World!" is the same as "Hello, World!". Other manipulations are possible, but through functions, which we will see later.

Booleans

Finally, to represent truth and falsehood, Go has a boolean type, which can be either true or false. They are very handy for controlling the flow of a program, as conditional statements will be of the form if condition { ... }, where condition is a boolean. There are also loops that will be executed as long as a condition is true.

They can be combined through "logical connectives":

  • && for the conjunction (and),
  • || for the disjunction (or),
  • ! for the negation (not).

Basically,

  • x && y is true if and only if x and y are true,
  • x || y is true if and only if x or y (or both) are true,
  • !x is true if and only if x is false.

© 2023 W3Basic. All rights reserved.

Follow Us: