Skip to content

Incorrect documentation: make([]int) without parameters is invalid Go syntax #859

@sammedsc45

Description

@sammedsc45

Issue

In the Go Slices tutorial (https://www.learn-golang.org/en/Slices), there's a statement that is factually incorrect:

"You can skip the capacity and length and only give the type to get an empty slice"

Followed by:

exampleSlice = make([]int)

Problem

make([]int) without any length or capacity parameters is not valid Go syntax. This code will not compile.

Solution

The statement and example should be corrected. To create an empty slice, the correct approaches are:

  1. Using zero value (nil slice):
var exampleSlice []int
  1. Using make with length 0:
exampleSlice := make([]int, 0)
  1. Using composite literal:
exampleSlice := []int{}

The make() function requires at least the type and length parameters for slices.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions