Go make function

Go make function

Go (Golang) the `make` function, to create slices, maps and channels

Fortunately, Go has many ways of memory allocation and value initialization, though, some of us don't know how to use them properly.

In this article, we are going to speak of the Go built-in function, make

How to use it, when, the parameters it does accept and more.

Make

In Go, make is a special built-in function, there is a similar one called new but that one is fairly different and has a different purpose.

The make function signature is as follows, it has 3 arguments, the first one is the type, the second is the length and the last is its capacity.

// package builtin
func make(t Type, size ...IntegerType) Type

According to the official documentation:

The make built-in function allocates and initializes an object of type slice, map, or chan (only). Like new, the first argument is a type, not a value. Unlike new, make's return type is the same as the type of its argument, not a pointer to it. The specification of the result depends on the type:

Slice: The size specifies the length. The capacity of the slice is equal to its length. A second integer argument may be provided to specify a different capacity; it must be no smaller than the length. For example, make([]int, 0, 10) allocates an underlying array of size 10 and returns a slice of length 0 and capacity 10 that is backed by this underlying array.

Map: An empty map is allocated with enough space to hold the specified number of elements. The size may be omitted, in which case a small starting size is allocated.

Channel: The channel's buffer is initialized with the specified buffer capacity. If zero, or the size is omitted, the channel is unbuffered.

At Glance

make function can take 3 parameters, but the first parameter is the only required one, which is the type, that we would return out of the function.

The make function, creates an underlying array for this type, and enables you to optionally define the starting length of the array, and the starting capacity.

  • A type

    • slice

    • map

    • channel

  • length

  • capacity

Lets break it down

Type

The first argument in the make function, is the type we intend to create, So it applies only to the 3 built-in reference types: slices, maps and channels.
It's a function that allocates and initializes the specified object type, as discussed, So that's the variable type we return.

Unlike new, which returns a pointer to the data it has created and allocated space for you.

Length

The optional second argument has to be of type integer, which is the length of the type we want to initialize. if we don’t specify anything it's initialized to 0, as for channel type, it would be an unbuffered channel.

Capacity

The capacity is optional, it would be the underlying array we allocate for this type, the array should be of size bigger than the length, otherwise, it would throw an error.

invalid argument: length and capacity swapped