Standard library / reflect

reflect.MakeSlice()

Creates a zero-initialized slice with the requested length and capacity. Expected stdout: '[0 0] 4\n'.

Formal Syntax & Structure

go syntaxgo
func MakeSlice(typ Type, len, cap int) Value

Parameters & Attributes

NameTypeDescription
typTypeRuntime type descriptor for the new value.
lenintInitial slice length.
capintSlice capacity, at least the length.

Return Value

Value

Creates a zero-initialized slice with the requested length and capacity.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() {  v := reflect.MakeSlice(reflect.TypeFor[[]int](), 2, 4)  fmt.Println(v.Interface(), v.Cap())}
Go Reference