Ashish Singh

Exploring the “New” and “Make” in Go: Features and Uses

Go, also known as Golang, is a statically typed, compiled programming language developed by Google. It has gained immense popularity in recent years, thanks to its simplicity, efficiency, and robustness. In this blog post, we will delve into two essential concepts in Go: new and make. These built-in functions play a crucial role in memory management and data structure creation in Go, making the language even more powerful and versatile.

The new Function

The new function in Go is used for memory allocation and object initialization. It returns a pointer to a newly created zeroed value of the specified type. Here's a breakdown of how new works:

p := new(Type)
  • Type: This should be the type of the object you want to create.
  • p: The result of the new function is a pointer to the zeroed value of Type.

Example Usage of new

Let's say we want to create a new integer and initialize it to zero using new:

var numPtr *int
numPtr = new(int)

// Now, numPtr points to a zeroed integer.
fmt.Println(*numPtr) // Output: 0

Use Cases for new

  1. Allocating Memory for a Struct: You can use new to allocate memory for a struct and initialize it to zero values.
type Person struct {
    Name string
    Age  int
}

personPtr := new(Person)
  1. Creating Pointers to Primitive Types: When you need to create a pointer to a primitive type like int, float64, or bool, new can be handy.
var floatPtr *float64
floatPtr = new(float64)

The make Function

The make function in Go is used for creating and initializing slices, maps, and channels. Unlike new, which works with any type, make is specific to these three built-in types. Here's how make is used:

make(T, size)
  • T: The type of the created value (slice, map, or channel).
  • size: The size or capacity of the value, depending on the type.

Example Usage of make

Creating a Slice

slice := make([]int, 5, 10)

In this example, we create a slice of integers with a length of 5 and a capacity of 10.

Creating a Map

m := make(map[string]int)

Here, we create an empty map with string keys and integer values.

Creating a Channel

ch := make(chan int)

This code creates an unbuffered channel for integers.

Use Cases for make

  1. Slices: When you need a dynamically sized, resizable array, use make to create slices. You can specify both the length and capacity.

  2. Maps: To create a map for key-value storage, use make to initialize it. Maps are often used for data indexing and lookup.

  3. Channels: Channels are essential for concurrent programming in Go. Use make to create channels for communication between goroutines.

Conclusion

In Go, the new and make functions serve crucial roles in memory allocation and data structure creation. While new is versatile and used for creating zeroed values of any type, make is specific to slices, maps, and channels. Understanding when and how to use these functions is fundamental for effective Go programming. As you explore Go further, you'll find that these tools are invaluable in building efficient and reliable software.

read more Synchronizing Concurrent Code with sync.Mutex in Go