#strange !!!

6 messages · Page 1 of 1 (latest)

humble idol
#

one := make([]int, 2,3)

creates a slice with length 2, capacity 3.

If you print one before the append, you will have [0, 0].

#

Length: refers to the number of items currently in the slice.
Capacity: refers to the maximum number of elements the slice can hold before it needs to be resized (the underlying array's size).

package main

import "fmt"

func main() {
    foo := make([]int, 5, 10)
    fmt.Println(foo)      // [0 0 0 0 0]
    fmt.Println(len(foo)) // 5
    fmt.Println(cap(foo)) // 10
}

When you create a slice with length 5, go will fill the array with zero values, that's why there are zeroes.

modest pawnBOT
#
func make(t Type, size ...IntegerType) Type

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.

velvet mirage
#

try to use better topic names.

bitter moss
velvet mirage
#

that's not an opinion, that's a suggestion.