Home  >  Article  >  Backend Development  >  Can golang map delete elements?

Can golang map delete elements?

(*-*)浩
(*-*)浩Original
2019-12-17 09:56:092744browse

Can golang map delete elements?

#Map is a built-in type in Go that binds keys and values ​​together. The corresponding value can be obtained by key.

How to create a map?

A map can be created by passing the types of keys and values ​​to the built-in function make. (Recommended learning: go) The syntax of

is:

make(map[KeyType]ValueType)

(Translator’s Note: The type of map is represented as map[KeyType]ValueType) For example:

personSalary := make(map[string]int)

delete () function

delete () function is used to delete elements of the collection, and the parameters are map and its corresponding key. Examples are as follows:

package main
import "fmt"
func main() {
    m := map[string]int{
        "a": 1,
        "b": 2,
        "c": 3,
    }
    fmt.Println("Deleting values")
    name, ok := m["a"]
    fmt.Println(name,ok)
    delete(m,"a")
    name,ok = m["a"]
    fmt.Println(name,ok)
}

The above is the detailed content of Can golang map delete elements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is golang map in order?Next article:Is golang map in order?