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

Can golang map delete elements?

(*-*)浩
Release: 2019-12-17 09:56:09
Original
2872 people have browsed it

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)
Copy after login

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

personSalary := make(map[string]int)
Copy after login

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)
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template