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) ValueParameters & Attributes
| Name | Type | Description |
|---|---|---|
| typ | Type | Runtime type descriptor for the new value. |
| len | int | Initial slice length. |
| cap | int | Slice 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())}