Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the key value of Map by Golang

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to exchange the key value of Map by Golang". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how to exchange the key value of Map by Golang" can help you solve the problem.

What is Map?

Map is an unsorted collection of key-value pairs, similar to the concept of a dictionary in Python, in the format map [keytype] valueType, which is a hash structure of key-value. The reading and setting of map is the same as that of slice, which is operated through key, except that the index of slice can only be int type, while map has many more types, which can be int, string and all types that fully define = = and! = operations.

Second, detailed code 1. Pair keying value

Map raw data:

MoMap: = mapping [string] int {"Zhang San": 21, "Li Si": 56, "Wang Wu": 23, "Zhao Liu": 45, "Zhou Qi": 32, "Chen Ba": 21, "Xu Jiu": 21, "Wang Shi": 16, "Wu San": 45, "Zheng Liu": 23, "Xu Qi": 43, "Li San": 16,}

The specific code is as follows (example):

/ / key-value swap / / input parameter: moMap map [string] int// return value: map [int] [] stringfunc reserveMap (moMap map [string] int) map [int] [] string {/ / establish a resMap with the same capacity as moMap / / because there may be multiple values corresponding to one Key / / string needs to be sliced [] string resMap: = make [] string Len (moMap)) / / traversing moMap / / k through for range means Key v is Value for k, v: = range moMap {/ / because it now corresponds to slice [] string / / use append to add multiple resMap [v] = append (resMap [v], k)} / / Program ends return resMap} 2. Make a call

The detailed code is as follows (example):

Package mainimport ("fmt") func main () {moMap: = map [string] int {"Zhang San": 21, "Li Si": 56, "Wang Wu": 23, "Zhao Liu": 45, "Zhou Qi": 32, "Chen Ba": 21, "Xu Jiu": 21, "Wang Shi": 16, "Wu San": 45, "Zheng Liu": 23, "Xu Qi": 43 "Li San": 16,} / / for k, v: = range moMap {fmt.Printf ("Key:% v, Value:% v\ n", k, v)} resMap: = reserveMap (moMap) fmt.Println ("reserve:") / / print for k, v: = range resMap {fmt.Printf ("Key:% v, Value:% v\ n", k) V)}} / / key-value exchange / / input parameter: moMap map [string] int// returned value: map [int] [] stringfunc reserveMap (moMap map [string] int) map [int] [] string {/ / establish a resMap with the same capacity as moMap / / due to the possibility of multiple values in the exchange, a Key / / string needs to be sliced [] string resMap: = make [] string Len (moMap)) / / traversing moMap / / k through for range means Key v is Value for k, v: = range moMap {/ / because it now corresponds to slice [] string / / use append to achieve the effect of adding multiple resMap [v] = append (resMap [v], k)} / / Program ends return resMap}

Summary

A simple exchange of key values is a prelude to familiarity with Golang Map data types.

PS:golang unordered key-value pair mappackage mainimport "fmt" func main () {/ * create the collection and initialize * / countryCapitalMap: = make (map [string] string) / * map insert key-value pair Corresponding capitals of each country * / countryCapitalMap ["France"] = "Paris" countryCapitalMap ["Italy"] = "Rome" countryCapitalMap ["Japan"] = "Tokyo" countryCapitalMap ["India"] = "New Delhi" / * use key to output value * / for country: = range countryCapitalMap {fmt.Println (country, "Capital is" CountryCapitalMap [country])} / * check whether the element exists in the collection * / capital, ok: = countryCapitalMap ["American"] / * if it is determined to be real, it exists, otherwise it does not exist * / / * fmt.Println (capital) * / * fmt.Println (ok) * / if (ok) {fmt.Println ("the capital of American is" Capital)} else {fmt.Println ("the capital of American does not exist")}} about "how Golang exchanges the keys of Map", that's all. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report