Standard library / reflect

reflect.Value.MapIndex()

Returns the mapped element or an invalid Value for a missing key, including lookup in a nil map. Expected stdout: '7 false\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) MapIndex(key Value) Value

Parameters & Attributes

NameTypeDescription
keyValueMap key type, or a struct tag key.

Return Value

Value

Returns the mapped element or an invalid Value for a missing key, including lookup in a nil map.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() {  m := reflect.ValueOf(map[string]int{"a": 7})  fmt.Println(m.MapIndex(reflect.ValueOf("a")).Int(), m.MapIndex(reflect.ValueOf("b")).IsValid())}
Go Reference