Standard library / reflect

reflect.MapOf()

Construct a map type. The key type must be comparable; this operation does not allocate a map. Expected stdout: 'map[string]int\n'.

Formal Syntax & Structure

go syntaxgo
func MapOf(key, elem Type) Type

Parameters & Attributes

NameTypeDescription
keyTypeComparable key type; an invalid key type panics.
elemTypeElement type of the resulting collection.

Return Value

Type

Construct a map type.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() { fmt.Println(reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(0))) }
Go Reference