Latest Code Tutorials

Golang make: How to Create Slice in Go

Golang slice make() function creates the empty slice. The empty slices are those slices that contain the empty array reference.

Golang make

Golang make() is a built-in slice function used to create a slice. The make() function takes three arguments: type, length, and capacity, and returns the slice. To create dynamically sized arrays, use the make() function.

The make() function allocates a zeroed array and returns a slice that refers to that array. The function assigns the underlying array with a size that is equal to the given capacity and returns the slice, which refers to the underlying array.


func make([]T, len, cap) []T

Golang slice is the flexible and extensible data structure to implement and manage data collections. Slices are made up of multiple items, all of the same type.

The slice is a segment of dynamic arrays that can grow and shrink as you see fit.

Implementation of Golang slice make()

Now, let’s create an empty slice.

// hello.go

package main

import "fmt"

func main() {

	// Creating an array of size 6
	// and slice this array  till 3
	// and return the reference of the slice
	// Using make() function
	var sliceA = make([]int, 3, 6)
	fmt.Printf("SliceA = %v, \nlength = %d, \ncapacity = %d\n",
		sliceA, len(sliceA), cap(sliceA))

	// Creating another array of size 6
	// and return the reference of the slice
	// Using make() function
	var sliceB = make([]int, 6)
	fmt.Printf("SliceB = %v, \nlength = %d, \ncapacity = %d\n",
		sliceB, len(sliceB), cap(sliceB))


SliceA = [0 0 0],
length = 3,
capacity = 6
SliceB = [0 0 0 0 0 0],
length = 6,
capacity = 6

When you use the make() function, one option you have is to specify the length of the slice. When you specify the length, the capacity of the slice is the same. In our example, we have defined the length and capacity of the slice.

The length of the slice is in the first example, 3, and the capacity is 6. In the second example, the length is 6, and the capacity is 6.

Golang slice literal

You can create a slice using a slice literal. The creation of the slice literal is just like an array literal, but with one difference, you are not allowed to specify the size of the slice in the square braces[].

var sliceA = []string{"Gandia", "Arturo", "Alicia"}

One thing to remember is that when you create the slice using a string literal, then it first creates the array, and after that, return the slice reference to it.


We can create a slice using Golang make() function and slice literal.

That’s it for this tutorial.

See also

How to convert slice to string

Golang slice append()

Golang string to rune

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.