diff --git a/_test/delete0.go b/_test/delete0.go new file mode 100644 index 00000000..54ac3167 --- /dev/null +++ b/_test/delete0.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + a := map[string]int{"hello": 1, "world": 3} + delete(a, "hello") + fmt.Println(a) +} + +// Output: +// map[world:3] diff --git a/interp/interp.go b/interp/interp.go index 2f273534..4527e301 100644 --- a/interp/interp.go +++ b/interp/interp.go @@ -155,6 +155,7 @@ func initUniverse() *Scope { "cap": &Symbol{kind: Bltn, builtin: _cap}, "close": &Symbol{kind: Bltn, builtin: _close}, "copy": &Symbol{kind: Bltn, builtin: _copy}, + "delete": &Symbol{kind: Bltn, builtin: _delete}, "len": &Symbol{kind: Bltn, builtin: _len}, "make": &Symbol{kind: Bltn, builtin: _make}, "new": &Symbol{kind: Bltn, builtin: _new}, diff --git a/interp/interp_test.go b/interp/interp_test.go index 40fd26bc..aee33cf9 100644 --- a/interp/interp_test.go +++ b/interp/interp_test.go @@ -1899,6 +1899,29 @@ func main() { // hello } +func Example_delete0() { + src := ` +package main + +import "fmt" + +func main() { + a := map[string]int{"hello": 1, "world": 3} + delete(a, "hello") + fmt.Println(a) +} +` + i := interp.New(interp.Opt{Entry: "main"}) + i.Use(stdlib.Value) + _, err := i.Eval(src) + if err != nil { + panic(err) + } + + // Output: + // map[world:3] +} + func Example_export0() { src := ` package main diff --git a/interp/run.go b/interp/run.go index 8de72a58..fffc79f9 100644 --- a/interp/run.go +++ b/interp/run.go @@ -1322,6 +1322,18 @@ func _close(n *Node) { } } +func _delete(n *Node) { + value0 := genValue(n.child[1]) // map + value1 := genValue(n.child[2]) // key + next := getExec(n.tnext) + var z reflect.Value + + n.exec = func(f *Frame) Builtin { + value0(f).SetMapIndex(value1(f), z) + return next + } +} + func _len(n *Node) { i := n.findex value := genValue(n.child[1])