Standard library / reflect
reflect.MakeMapWithSize()
Creates an initialized map using a requested initial size hint, not a maximum size. Expected stdout: '3\n'.
Formal Syntax & Structure
go syntaxgo
func MakeMapWithSize(typ Type, n int) ValueParameters & Attributes
| Name | Type | Description |
|---|---|---|
| typ | Type | Runtime type descriptor for the new value. |
| n | int | Requested size, length, capacity or growth amount, as specified by the operation. |
Return Value
Value
Creates an initialized map using a requested initial size hint, not a maximum size.
Working Example
Example Snippetgo
package main
import ( "fmt" "reflect")
func main() { m := reflect.MakeMapWithSize(reflect.TypeFor[map[string]int](), 2) for _, k := range []string{"a", "b", "c"} { m.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(1)) } fmt.Println(m.Len())}