golang for map's judgment delete operation example

  • 2020-06-07 04:36:47
  • OfStack

This article illustrates golang's judgment against map and the deletion operation. To share for your reference, specific as follows:

map is an ES6en-ES7en relationship. make is generally used to initialize memory, which helps to reduce the number of memory allocations for subsequent new operations. If 1 is initially defined but not initialized with make, an error will be reported.

package main
import (
"fmt"
)
func main(){
var test =  map[string]string{" The name ":" li 4"," gender ":" male "}
name,ok := test[" The name "] // if key There are , the name = li 4 . ok = true, Otherwise, ok = false
if ok{
fmt.Println(name)
}
delete(test," The name ")// Delete as the name is key It doesn't matter if it doesn't exist
fmt.Println(test)
        var a map[string]string
        a["b"] = "c"// This will report an error, to initialize the memory first
        a = make(map[string]string)
        a["b"] = "c"// It's not wrong
}

I hope that this article has been helpful in Go programming.


Related articles: