Using Make and New with Slices in Go

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Overview
  4. Using Make with Slices
  5. Using New with Slices
  6. Conclusion

Introduction

Welcome to this tutorial on using Make and New with slices in Go! In this tutorial, we will explore two different ways to create slices in Go using the make and new built-in functions. By the end of this tutorial, you will understand the differences between make and new when working with slices, and be able to use them effectively in your Go programs.

Prerequisites

Before starting this tutorial, you should have a basic understanding of Go programming language syntax and concepts. You should also have Go installed on your machine. If you haven’t installed Go yet, you can download it from the official Go website (https://golang.org) and follow the installation instructions specific to your operating system.

Overview

In Go, a slice is a dynamically-sized, flexible view into an underlying array. It provides a convenient way to work with sequences of elements. Both make and new functions can be used to create slices, but they have different purposes and behavior.

The make function is used to create a slice by specifying its type, length, and capacity. It initializes the slice with the specified length and capacity, and returns a reference to it. The length represents the number of elements present in the slice, while the capacity represents the maximum number of elements it can hold without reallocating memory.

On the other hand, the new function is used to create a slice by specifying its type only. It returns a pointer to a newly allocated, zero-initialized slice header, but the underlying array is not initialized. This means that the resulting slice has a length and capacity of 0.

Now let’s dive into the details of using make and new with slices in Go.

Using Make with Slices

To create a slice using the make function, you need to specify the type of the elements, the length, and the capacity of the slice. The syntax for using make with slices is as follows:

slice := make([]Type, length, capacity)

Here, Type represents the type of the elements that the slice will hold. length is the number of elements you want to initialize the slice with, and capacity is the maximum number of elements the slice can hold.

Let’s look at an example of creating a slice of integers using make:

package main

import "fmt"

func main() {
    // Create a slice of integers with length 3 and capacity 5
    numbers := make([]int, 3, 5)

    // Print the length, capacity, and elements of the slice
    fmt.Println("Length:", len(numbers))
    fmt.Println("Capacity:", cap(numbers))
    fmt.Println("Elements:", numbers)
}

Output:

Length: 3
Capacity: 5
Elements: [0 0 0]

In the example above, we used make to create a slice of integers with a length of 3 and a capacity of 5. The resulting slice numbers is initialized with 3 zero-valued elements ([0 0 0]). The len function returns the length of the slice (3), and the cap function returns the capacity (5).

Using New with Slices

To create a slice using the new function, you only need to specify the type of the elements. The syntax for using new with slices is as follows:

slice := new([]Type)

Here, Type represents the type of the elements that the slice will hold.

Let’s look at an example of creating a slice of strings using new:

package main

import "fmt"

func main() {
    // Create a slice of strings
    var names []string = new([]string)

    // Print the length, capacity, and elements of the slice
    fmt.Println("Length:", len(names))
    fmt.Println("Capacity:", cap(names))
    fmt.Println("Elements:", names)
}

Output:

Length: 0
Capacity: 0
Elements: &[]

In the example above, we used new to create a slice of strings. The resulting slice names has a length and capacity of 0. Notice that we assigned the result of new([]string) to a variable of type []string using the var keyword.

Conclusion

In this tutorial, we explored how to use make and new with slices in Go. We learned that make is used to create a slice with a specified length and capacity, while new is used to create a slice with a length and capacity of 0.

By using make, we can initialize a slice with elements and specify the capacity upfront. This is useful when we know the expected size of the slice or want to avoid reallocations. On the other hand, new is useful when we want to allocate a slice header without initializing the underlying array.

Remember to choose the appropriate function based on your requirements when creating slices in Go. Happy coding!