Secondary Data Types in Golang

Learn via video courses
Topics Covered

Overview

Golang, usually known as Go, is a programming language that supports a wide range of data types for storing and manipulating data. These data types are divided into primary and secondary data types. Secondary data types in Golang are built on top of primary data types. They include strings, slices, arrays, structs, maps, channels, and many more. These data types are used to store more complex data structures and perform advanced operations, which aids developers in developing complex and advanced applications.

What are the Secondary Data Types in Golang?

Secondary data types in Golang include strings (for text), arrays (for storing similar items), slices (similar to arrays with variable size), structs (for creating custom types), maps (for storing key-value pairs), channels (for communicating between concurrently running functions), and functions (for performing a specific task). Secondary data types are used to store more complex data structures and perform advanced operations, allowing developers to write more expressive and maintainable code while designing complicated and advanced applications.

Array

Arrays in the Go language are quite similar to those seen in other programming languages. It is a fixed-length, ordered collection of the same type of elements. The length of an array is defined when it is created and cannot be modified. Array elements can be of several data types, such as Int, String, Boolean, and others.

Syntax

There are two methods to declare an array in Go:

  1. Using the var keyword: Arrays in the Go programming language are declared with the var keyword of a certain type, name, size, and element.

  2. Using := operator: Arrays in the Go programming language can also be declared using a := operator. It is more flexible than the previous declaration.

Example

This example uses the var keyword and := operators to declare two arrays (array1 and array2):

Output

Struct

In the Go language, the struct is a collection of data fields with declared data types. It is similar to a class in other object-oriented programming languages. Using struct, you can declare and construct your data types in Golang. Each field within a struct has a name and a type, and the fields are used to store data.

Syntax

In Go language, we use the type and struct keywords to declare a structure:

Example

In the following example, we declared a struct type Student with the following members: name, roll_no, and marks. The name is a string type, whereas roll_no and marks are int types. We use the dot operator (.) between the structure variable name and the structure member to access any structure member.

Output

Pointers

Pointers in the Go programming language or Golang is a variable that holds the memory address of another variable. The & operator is used to get a variable's memory address, while the * operator is used to dereference a pointer and access the value stored at the memory address it holds. We can operate with memory addresses directly using pointers. For example, we can use pointers to access and alter the values of variables in memory.

Syntax

The * operator is used before the variable type to declare a pointer. Here is an example of a pointer declaration.

Example

In the following example, we have created the ptr pointer variable, which contains the memory location of the val variable. The term *int in the pointer declaration indicates that the pointer variable is of the type int

Output

Slices

In the Go language, Slices are similar to arrays, but they are more powerful and flexible than an array. A slice is a flexible and extensible lightweight data structure used to implement and manage data collections. Slices are made up of numerous items of the same type. A slice is a dynamic array segment that can expand and shrink as needed. Slices the ave capacity and length properties. Here, length denotes the total number of items in the array, while capacity represents the maximum size to which it can expand.

Synatx The [] operator is used to declare a slice after defining the type of its elements. There are various ways to create a slice in Go, one of which is shown below.

Example

In the following example, we have created a slice named val. In this case, int specifies that the slice val can only store integers. We printed the slice, as well as its length and capacity.

Output

Maps

In the Go language, the map data structure is a collection of key-value pairs, where each key is unique. In maps, the 'key' serves as an indexer for the rest of the data. The len function determines the size of the map (the number of key-value pairs).  You can only add value to a map once it has been initialized. If you try to insert any value into an uninitialized map, the compiler will give an error.

Syntax

Maps in Go can be created using the var keyword and the := operator. The syntax for both is shown below.

Example

In the following example, we have created a map and printed key valueslues. We also printed the array's length by using the len function.

Output

Functions

In the Go language, A function is a collection of statements that exist within a program to perform a specific task. Functions can accept Zero or more parameters and return Zero or more values. Go language includes various built-in functions, such as len and make, and you can even write your own.

Syntax

In the Go programming language, the basic syntax for creating a function is as follows:

Example

In the following example, we have created a function named Multiply_Numbers(). The function multiplies two integers and returns the result.

Output

Channels

Channels are used to communicate between goroutines in the Go programming language. We already know that goroutines are used to create concurrent applications. By default, the channel is bidirectional, which implies that goroutines can transmit and receive data over the same channel. Channels are typed, so you can only transmit and receive data of a specified type across a given channel.

Syntax

In the Go programming language, the basic syntax for creating a channel is as follows:

Example

In the following example, we create a channel named ch1 with the type string and start the greeting function as a goroutine. We utilized the <- operator to perform operations on the channel to send and receive data.

Output

Conclusion

  • Secondary data types in Golang are built on top of primary data types. They include strings, slices, arrays, structs, maps, channels, and many more.
  • An array is a fixed-length, ordered collection of the same type of elements. The length of an array is defined when it is created and cannot be modified.
  • In the Go language, the struct is a collection of data fields with declared data types.
  • Pointers in the Go programming language or Golang is a variable that holds the memory address of another variable.
  • A slice is a flexible and extensible lightweight data structure used to implement and manage data collections.
  • In the Go language, the map data structure is a collection of key-value pairs, where each key is unique.
  • A function in Go is a collection of statements that exist within a program to perform a specific task.
  • Functions can accept Zero or more parameters and return Zero or more values.
  • Channels are used to communicate between goroutines in the Go programming language.