Primary Data Types in Golang

Learn via video courses
Topics Covered

Overview

The term "data type" in the Go programming language describes the kind of value that a variable can store or the kind of action that a function or method takes. Data types are crucial in Go (Golang) because they offer a means of classifying and labeling various forms of data, which enables the Go runtime to comprehend how to handle the data and perform actions on it. The data type of a variable can only hold values of that particular type if it is explicitly defined, preventing problems brought on by incompatible types. Because it is now apparent what kind of data a variable or function is intended to operate with, the code is easier to read and maintain.

What are the Primary Data Types in Golang?

Data types are categorized into four categories:

  • Primary types: include integers, floating-point numbers, Booleans, and strings.
  • Aggregate types: include arrays and structs.
  • Reference types: include pointers, slices, maps, and channels.
  • Interface types: allow for polymorphism in Go, and include any type that implements a set of methods.

In this article, we will learn about the first category i.e. Primary Data Types in Golang.

Primary Data Types

In Go (also known as Golang), primary or basic data types are the building blocks of the language and include:

Integers: Go has several integer types, including int, int8, int16, int32, int64, and uint (unsigned integers). The specific type used depends on the size of the number and whether it is signed or unsigned.

Floating-point numbers: Go has two types for floating-point numbers: float32 and float64. The difference between the two is the size and precision of the numbers they can represent.

Booleans: Go's Boolean type is called bool and can only have the values true or false.

Strings: Go's string type is used to represent sequences of characters. Strings in Go are immutable, meaning that once a string is created, it cannot be modified.

All these basic types have specific properties and methods based on their category.

Let's learn about them in a detail.

Numbers

In the Go programming language, an integer data type is a numerical data type that can store positive or negative whole numbers. Go supports several different types of integers, including int, int8, int16, int32, and int64.

Integers

  1. int: This is the default integer type and is either 32 or 64 bits depending on the system architecture. It is the most common integer type and can store a wide range of values. For example, on a 64-bit system, an int variable can store values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807

For Example:

Output

Run the code:

  1. int8: This type is specifically 8 bits and can store values between -128 and 127.

For Example:

Output

Run the code:

  1. int16: This type is specifically 16 bits and can store values between -32768 and 32767

For Example:

Output

Run the code:

  1. int32: This type is specifically 32 bits and can store values between -2147483648 and 2147483647.

For Example:

Output:

Run the code:

  1. int64: This type is specifically 64 bits and can store values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

For Example:

Output:

Run the code:

Additionally, Go also has unsigned integer types, such as uint, uint8, uint16, uint32, and uint64. These types can only store positive values and have the same size as their signed counterparts.

Floating-Point Numbers

  • In the Go programming language, a floating-point number is a numerical data type that can store decimal numbers. Go supports two types of floating-point numbers: float32 and float64.
  • The float32 type is a single-precision floating-point number, which means it can store numbers with up to 7 decimal digits of precision.
  • The float64 type is a double-precision floating-point number, which can store numbers with up to 15 decimal digits of precision.

Here's an example of how to declare and use floating-point numbers in Go:

Output:

Run the code:

Explanation of the above code:

  • In this example, we declared a float32 variable named pi and assigned it the value 3.14.
  • We also declared a float64 variable named e and assigned it the value 2.71828. We then used the fmt. Println function to print the values of the variables.

It's important to note that floating-point numbers are not exact, they are an approximation of real numbers, so they might not be exactly equal to the number you expect them to be.

Complex Numbers

In the Go programming language, a complex number is a numerical data type that consists of a real part and an imaginary part. Complex numbers are denoted with the complex64 and complex128 types, where complex64 is a 32-bit complex number and complex128 is a 64-bit complex number.

You can create a complex number by using the complex() built-in function, which takes two arguments, a real part and an imaginary part.

Example:

Output:

Run the code:

Sizes of Numeric Types

In Go, the sizes of numeric types can vary depending on the architecture of the system on which the code is running. However, the sizes of numeric types are defined in the language specification and are consistent across all platforms.

Here is a list of the sizes of numeric types in Go:

  • The sizes of int and uint types are not fixed in Go. They are determined by the architecture of the system on which the code is running. On a 32-bit system, int and uint will be 32 bits, while on a 64-bit system, they will be 64 bits.
  • Also, Go provides an "int" and "uint" type that are platform-dependent and are either 32 bits or 64 bits in size.
  • In addition, Go provides a "byte" type that is an alias for uint8 and a "rune" type that is an alias for int32. These types are commonly used for representing characters and text.
  • At last, Go provides a wide range of numeric types with different sizes, providing flexibility in choosing the appropriate type for different situations. However, it's important to keep in mind the platform-dependent nature of int and uint types and to use them with caution.

Booleans

In the Go programming language, a Boolean data type is a data type that can have only two values: true or false. Booleans are often used in conditional statements and loops to control the flow of a program.

Here's an example of how to use a Boolean variable in Go:

Output:

Run the code:

Explanation of the above code:

In the above example, we declare two Boolean variables isTrue and isFalse and assign the values true and false respectively. The fmt.Println function is then used to print the values of these variables to the console.

Strings

Strings in Go are a sequence of characters, represented by the string data type. Strings are immutable, meaning that once a string is created, it cannot be modified. To create a string, you can use either double quotes (") or backticks to define a string literal.

Example of a string literal:

Strings can be altered using several methods. It will internally generate a new string.

1. Concatenation

You can also use the + operator to concatenate strings.

Example:

2. Indexing

Strings can be indexed to access individual characters.

Example:

3. Comparison Operator

Strings can be compared using the comparison operators (==, !=, <, >, <=, >=).

Example:

4. Slicing

You can extract a portion of a string using slicing.

Example:

5. Length

The built-in len() function can be used to get the length of a string.

Example:

Go provides several built-in functions for working with strings, such as strings. Contains(), strings.Replace(), strings.ToLower(), strings.ToUpper(), etc.

You can also use the package fmt to perform operations like Printf, Sprintf, Fprintf, etc on strings.

Example:

Output:

Run the code:

Explanation of the above code:

  • In this example, we first declare a string variable name with the value "John Doe". We then use the fmt. Println function to print the value of the name variable.
  • Next, we reassign a new value "Jane Smith" to the name variable and print it again using the fmt.Println function.

You can also use the short variable declaration operator := to declare and initialize a string variable in one line

Example:

Output:

[Run the code:](https://go.dev/play/p/xTu_GUJ17ks"{rel=nofollow noopener}")

Example:

Output:

Explanation of the code:

  • String declaration and printing: A string variable is declared and assigned a value of "Hello, World!", which is then printed using fmt.Println.
  • Concatenating strings: Two string variables, firstName, and lastName, are declared and concatenated to form a full name.
  • String length: The length of the string myString is determined using the len function.
  • Accessing individual characters: A specific character within the string myString is accessed and printed.
  • Checking for substring: The strings.Contains function is used to check if the string myString contains the substring "World".
  • Replacing a substring: The strings. Replace function is used to replace the substring "World" with "Go"` in the string myString.
  • Final string printing: The modified string is printed.

Conclusion:

  • Data types in Go are important because they determine the type of value that a variable can hold and the type of operation that a function or method performs.
  • Additionally, Go's strict type system also helps to prevent errors that may occur during runtime and allows for more efficient memory management, which can lead to more efficient and performant code.
  • We have seen, an integer data type is a numerical data type that can store positive or negative whole numbers. Go supports several different types of integers, including int, int8, int16, int32, and int64.
  • The float64 data type is a double-precision floating-point number, which means it uses 64 bits of storage to represent a number. It can represent values with a precision of about 15-17 decimal digits.
  • In Go, a string is a sequence of characters, It's an immutable data type, Strings can be concatenated and indexed, You can use comparison operators and the built-in len() function to get the length of a string, Go provides several built-in functions for working with strings, you can use package fmt to perform operations on strings.