golang maps code example
Example 1: length of map golang
package main
import "fmt"
func main() {
var employee = make(map[string]int)
employee["Mark"] = 10
employee["Sandy"] = 20
employeeList := make(map[string]int)
fmt.Println(len(employee))
fmt.Println(len(employeeList))
}
Example 2: initialize map in golang
m := make(map[string]int)
m["Dio"] = 3
m["Jonathan"] = 1
Example 3: go add to map
m := make(map[string]int)
m["numberOne"] = 1
m["numberTwo"] = 2
Example 4: golang map
package main
import (
"fmt"
)
type User struct {
Name string
Age int
}
func main() {
var person = map[string]string{
"name": "john doe",
"age": "23",
}
var profile = make(map[string]string)
profile["name"] = "jane doe"
profile["age"] = "23"
var user = make(map[string]interface{})
user["name"] = "peter parker"
user["age"] = 30
var users = []map[string]interface{}{
{"name": "monkey d lufy", "age": 19},
{"name": "trafagar d law", "age": 23},
{"name": "nico robin", "age": 20},
}
var userStruct = map[string]User{
"name": {Name: "Monkey D Lufy"},
"age": {Age: 19},
}
fmt.Println(person)
fmt.Println(profile)
fmt.Println(user)
fmt.Println(users)
fmt.Println(userStruct)
}
Example 5: what is the use of map in golang
Search Results
Featured snippet from the web
In Go language, a map is a powerful, ingenious, and a versatile data structure. Golang Maps is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update or delete with the help of keys. It is a reference to a hash table.