Skip to content

Commit

Permalink
Merge pull request #1983 from xushiwei/q
Browse files Browse the repository at this point in the history
mini spec: builtin make
  • Loading branch information
xushiwei authored Sep 17, 2024
2 parents f363191 + 6e66f55 commit 110b32c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion doc/spec-mini.md
Original file line number Diff line number Diff line change
Expand Up @@ -1554,4 +1554,30 @@ var z complex128

Making slices and maps

TODO
The built-in function `make` takes a type `T`, optionally followed by a type-specific list of expressions. The [core type]() of `T` must be a slice or map. It returns a value of type `T` (not `*T`). The memory is initialized as described in the section on [initial values]().

```go
Call Core type Result

make(T, n) slice slice of type T with length n and capacity n
make(T, n, m) slice slice of type T with length n and capacity m

make(T) map map of type T
make(T, n) map map of type T with initial space for approximately n elements
```

Each of the size arguments `n` and `m` must be of [integer type](#numeric-types), have a [type set](#interface-types) containing only integer types, or be an untyped constant. A constant size argument must be non-negative and [representable]() by a value of type `int`; if it is an untyped constant it is given type `int`. If both `n` and `m` are provided and are constant, then `n` must be no larger than `m`. For slices, if `n` is negative or larger than `m` at run time, a [run-time panic]() occurs.

```go
s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100
s := make([]int, 1e3) // slice with len(s) == cap(s) == 1000
s := make([]int, 1<<63) // illegal: len(s) is not representable by a value of type int
s := make([]int, 10, 0) // illegal: len(s) > cap(s)
m := make(map[string]int, 100) // map with initial space for approximately 100 elements
```

Calling make with a map type and size hint `n` will create a map with initial space to hold `n` map elements. The precise behavior is implementation-dependent.

### Min and max

TODO

0 comments on commit 110b32c

Please sign in to comment.