Standard library / reflect
reflect.Value.SetMapIndex()
Inserts or updates a map entry using assignable values. An invalid element Value deletes the key; a typed zero element stores zero. Expected stdout: 'true\n0\n'.
Formal Syntax & Structure
go syntaxgo
func (v Value) SetMapIndex(key, elem Value)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| key | Value | Assignable map key value. |
| elem | Value | Assignable map element, or an invalid Value to delete the entry. |
Working Example
Example Snippetgo
package main
import ( "fmt" "reflect")
func main() { m := reflect.ValueOf(map[string]int{"a": 7}) k := reflect.ValueOf("a") m.SetMapIndex(k, reflect.ValueOf(0)) fmt.Println(m.MapIndex(k).IsValid()) m.SetMapIndex(k, reflect.Value{}) fmt.Println(m.Len())}