In this article we will learn about some of the frequently asked GO programming questions in technical like “dictionary golang” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on go was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in GO. Below are some solution about “dictionary golang” Code Answer’s.
initialize map in golang
xxxxxxxxxx
1
// By default maps in Go behaves like a default dictionary in python
2
m := make(map[string]int)
3
4
m["Dio"] = 3
5
m["Jonathan"] = 1
dictionary in golang
xxxxxxxxxx
1
m := make(map[string]float64)
2
3
m["pi"] = 3.14 // Add a new key-value pair
4
m["pi"] = 3.1416 // Update value
5
fmt.Println(m) // Print map: "map[pi:3.1416]"
6
7
v := m["pi"] // Get value: v == 3.1416
8
v = m["pie"] // Not found: v == 0 (zero value)
9
10
_, found := m["pi"] // found == true
11
_, found = m["pie"] // found == false
12
13
if x, found := m["pi"]; found {
14
fmt.Println(x)
15
} // Prints "3.1416"
16
17
delete(m, "pi") // Delete a key-value pair
18
fmt.Println(m) // Print map: "map[]"
dictionary golang
xxxxxxxxxx
1
package main
2
3
import (
4
"fmt"
5
)
6
7
func main() {
8
dict := map[interface{}]interface{} {
9
1: "hello",
10
"hey": 2,
11
}
12
fmt.Println(dict) // map[1:hello hey:2]
13
}
14
go get from map
xxxxxxxxxx
1
var id string
2
var ok bool
3
if x, found := res["strID"]; found {
4
if id, ok = x.(string); !ok {
5
//do whatever you want to handle errors - this means this wasn't a string
6
}
7
} else {
8
//handle error - the map didn't contain this key
9
}