comicsmili.blogg.se

Golang maps godocs
Golang maps godocs




golang maps godocs

If you would like to know more about comparable types, please visit Even user-defined types such as structs can be keys. All comparable types such as boolean, integer, float, complex, string. It's not necessary that only string types should be keys. The program prints employeeSalary map contents: map Later one more element with key mike is added. The above program declares employeeSalary and adds two elements to it during the declaration itself. It is also possible to initialize a map during the declaration itself. The above program prints, employeeSalary map contents: map We have added three employees steve, jamie and mike and their corresponding salaries. package mainįmt.Println("employeeSalary map contents:", employeeSalary) The program below adds some new employees to the employeeSalary map. The syntax for adding new items to a map is the same as that of arrays. Since we have not added any elements to the map, it's empty. The program above creates a map named employeeSalary with string key and int value. The above line of code creates a map named employeeSalary which has string keys and int values. The following is the syntax to create a new map.

Golang maps godocs how to#

How to create a map?Ī map can be created by passing the type of key and value to the make function. Maps are similar to dictionaries in other languages such as Python. The name of the employee can be the key and the salary can be the value. A map will be a perfect fit for this use case. We are looking for a data structure to store the salary of each employee. For simplicity, let's assume that the first name of all these employees is unique. Let's take the example of a startup with a few employees. What is a map?Ī map is a builtin type in Go that is used to store key-value pairs.






Golang maps godocs