Published in · 3 min read · Jan 4, 2021
--
What is actually a map in Golang?.
There’s no such specific data type in Golang called map; instead, we use the map
keyword to create a map with keys of a certain type and values of another type (or the same type).
The value assigned here is a map literal. It takes the form:
TYPE{
KEY: VALUE,
...
}
Var menuprice map[string]int
In this example, we declare a map that has string
for its keys, and int for its values.
Golang map key types?? key vs values
What types of keys can a map have? Could we, for example, make a map whose keys are slices, or functions? The answer is no because the keys must be comparable that is, the ==
and !=
operators must be defined for that type. All the basic types (string
, int
, and so on) are comparable; so are structs. Functions and slices are not, and nor are maps, so we can't have a map whose key is another map!
There’s no such restriction on map values; they can be any type.
if we don’t have any values to put in the map yet, we can initialize it with an empty map literal:Var menuprice map[string]int{}, but we can use: we can’t read or write to anil
Now we know how to write key and values for the map, how do we actually store and retrieve values in the Go map?
Let us check whether a key exists in the map??
To know whether or not a Go map key exists, and there’s a special syntax for that:
menuprice, ok := menu["cheeseburger"]
if ok {
return fmt.Sprintf("Yes, cheeseburger is on the menu;%f", menuprice)
}
return fmt.Sprint("no any cheeseburger!!!")
The ok
value will betrue
if the key is found, and false
otherwise. We can ignore the first value altogether using the blank identifier (_
):_, ok := menu["cheeseburger"]
Maps with boolean as value
Suppose we have a map called inStock
which represents our presentstock:
var inStock = map[string]bool{
"eggs": true,
"buffsausage": true,
"chickensausage": true,
}
How can we now determine whether a given item, is in stock? We could use the value, ok
syntax to set a variable based on whether the key existed, and then check the variable:
if _, ok := inStock[eggs]; ok {
return fmt.Sprintf("is in stock", eggs)
}
return fmt.Sprintf("Sorry, out of stock", eggs)
Iteration over map
Using the range
operator: we can iterate over a map is to read each key-value pair in a loop.
for x, y:= range instock{
fmt.Println(x,y)
}
Each time around the loop is set to the next key and is set to the corresponding value.
Append map
var dishes []string
for dish:= range instock{
dishes = append(dishes, dish)
}
fmt.Println(dishes)
sorteddishes:= sort.Strings(dishes)
fmt.Println(sortdishes)
map[string]interface{}
what is interface{}?
- it’s the interface that specifies no methods at all. Now that doesn’t mean that
interface{}
values must have no methods; it simply doesn't say anything at all about what methods they may or may not have. - What is the use of it if it doesn’t hold any method then?.This is why it’s useful: it can refer to anything! The type
interface{}
applies to any value. A variable declared asinterface{}
can hold a string value, an integer, any kind of struct, a pointer to anos.
Eg:
menus := map[string]interface{}{
"icecream": "delicious",
"eggs": struct {
price float64
}{"chicken", 1.75},
"chickenSteak": true,
}
Let’s look at another example:
type Person struct {
Name string
Age int
Hobbies []string
}
person:=map[string]interface{
"name":"John",
"age":29,
"hobbies":[
"martial arts",
"breakfast foods",
"piano"
]
}
When to use map[string]interface{}?
- The “map of string to empty interface” type is very useful when we need to deal with data; arbitrary JSON data(the data in any format)of an unknown schema.
I think the research of mine will be pretty helpful when anyone needs to deal with interface in golang.
Thank you !!! .. Feedback will be highly appreciated.