Golang Make Function: Choosing Between Make and New

Author

Reads 160

Programming Code on Screen
Credit: pexels.com, Programming Code on Screen

In Go, the `make` function is a crucial tool for creating slices and maps. It's often compared to the `new` function, but they serve different purposes.

The `make` function is used to create a new slice or map with a specified capacity and length. This is particularly useful when you need to create a slice or map that will grow dynamically.

One key difference between `make` and `new` is that `make` initializes the underlying data structure, whereas `new` only allocates memory. This means that using `make` is generally safer and more efficient.

What is the Make Function?

The make function in Go is used for memory allocation and initialization, allowing us to create slices, maps, and channels.

It's primarily used for complex types that require proper initialization before use, unlike basic data types.

The make function returns an initialized value, not a pointer, which is not zeroed.

This is particularly useful when you know the starting amount of resources you'll be inserting into these data structures.

Credit: youtube.com, Functions in golang

For example, make([]int, 5) creates a slice of integers with a length of 5, where all elements within are initialized to zero.

In situations where you want to prepare a channel, map, or slice, make is the optimal choice.

It removes the need for reallocation when setting up the contents of a structure.

The make function allocates and initializes non-zeroed memory for built-in data structures like slices, maps, and channels.

Unlike new, which only allocates memory, make also initializes the memory with a non-zero value that is dependent on the type.

For instance, make can be used to create a slice with a specified length and capacity, a map, or a channel with a specified buffer size.

This is especially useful when you need to create a slice with a specific length and capacity, like make([]int, 10, 100), which creates a slice of integers with a length of 10 and a capacity of 100.

Basic Usage and Examples

Credit: youtube.com, Golang Course | Lesson 4 | Functions

The make function in Go is a special function that can take a different number of types and arguments. It returns an instance of the type passed as the first argument.

You can create a slice using the make() function, which sets the initial length and capacity of the slice. A slice consists of three components: a pointer to the data, a length, and a capacity.

If you want to create a slice with an initial length and capacity, you can specify both values in the make() function. For example, to create a slice of integers with an initial length of 5 and a capacity of 10, you'd use:

If you omit the capacity, it defaults to the length, making it easier to create a slice with the desired length.

Make vs New

In Go, the make and new functions are often used together, but they serve different purposes. The key difference lies in initialization and the types they serve.

Credit: youtube.com, Make vs New in Golang

Make is used for the initialization of slices, maps, and channels, allocating memory and initializing the underlying structure. It's a way to create these data structures and have them ready to use right away. New, on the other hand, allocates memory for a given type and returns a pointer to the zero value of that type.

Here are the key differences between make and new:

  • Purpose: new allocates memory and returns a pointer to it, initializing the memory to zero values of the specified type. make, however, is used to initialize slices, maps, and channels, not just allocate memory.
  • Return Type: new(T) returns a pointer to the newly allocated memory (*T), while make(T, args) returns an initialized value of type T.
  • Usage: new is generally used when you need a pointer to a value of a certain type. make is used when you need to initialize slices, maps, or channels, which require not just memory allocation but also initialization to their zero values.

What Is the Difference Between New and Old?

In Go, new and make are two built-in functions that deal with memory allocation, but they serve different purposes.

New allocates memory for a given type and returns a pointer to the zero value of that type. For example, new(int) returns a pointer to the zero value of int.

Make, on the other hand, is used for the initialization of slices, maps, and channels. It not only allocates memory but also initializes the underlying structure.

Here's a quick comparison of the two:

Make is a way to create a slice, map, or channel that's ready to use right away, while new is a way to create a pointer without having to declare and initialize a separate variable.

Make vs New

Workplace with modern laptop with program code on screen
Credit: pexels.com, Workplace with modern laptop with program code on screen

Make and new are two built-in functions in Go that deal with memory allocation, but they serve different purposes.

The key difference between make and new lies in initialization and the types they serve. Make is used for the initialization of slices, maps, and channels, allocating memory and initializing the underlying structure, while new allocates memory for a given type and returns a pointer to the zero value of that type.

Make can only be used to create slices, maps, and channels, but it's optimized for these specific data types. On the other hand, new can be used to create pointers to any data type.

Here are the key differences between make and new:

  • Purpose: new allocates memory and returns a pointer to it, initializing the memory to zero values of the specified type.
  • Return Type: new(T) returns a pointer to the newly allocated memory (*T), while make(T, args) returns an initialized value of type T.
  • Usage: new is generally used when you need a pointer to a value of a certain type, while make is used when you need to initialize slices, maps, or channels.

When to Use

When you're working with slices, maps, or channels in Go, use the `make` function to initialize them.

You need to use `make` because these types require more than just simple memory allocation.

For example, when working with slices, you need to specify the length and capacity of the slice, which is exactly what the `make` function does.

Credit: youtube.com, Golang Tutorial #17 - Advanced Function Concepts & Function Closures

When working with structs, use a pointer to directly manipulate the fields of the struct.

You can use the `new` function to create a pointer to a new variable, which is particularly useful when working with structs.

Here's a quick summary of when to use each:

  • Use `make` when working with slices, maps, or channels.
  • Use `new` when you need a pointer to a new variable, especially when working with structs.

Frequently Asked Questions

How do you create a function in Golang?

To create a function in Golang, use the `func` keyword followed by a name and parentheses, then define the function's behavior inside curly braces. This simple syntax allows you to write reusable code that can be called from anywhere in your program.

What is the make command in Go?

The make() function in Go is used for memory allocation and initialization, creating slices, maps, and channels. It's a powerful tool for working with Go's most versatile data types.

Judith Lang

Senior Assigning Editor

Judith Lang is a seasoned Assigning Editor with a passion for curating engaging content for readers. With a keen eye for detail, she has successfully managed a wide range of article categories, from technology and software to education and career development. Judith's expertise lies in assigning and editing articles that cater to the needs of modern professionals, providing them with valuable insights and knowledge to stay ahead in their fields.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.