How to retrieve value for a key from a map in different ways in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to retrieve value for a key from a map in different ways using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
##############SOURCE CODE#########################
package main
import "fmt"
func main() {
// Declare a map of string keys and integer values
m := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
// Retrieve the value for a key using the index operator
one := m["one"]
fmt.Println("Value for 'one':", one)
// Retrieve the value for a key using the built-in function make
two, ok := m["two"]
if ok {
fmt.Println("Value for 'two':", two)
} else {
fmt.Println("'two' is not a key in the map")
}
// Retrieve the value for a key using the comma ok idiom
three, ok := m["three"]
if ok {
fmt.Println("Value for 'three':", three)
} else {
fmt.Println("'three' is not a key in the map")
}
// Retrieve the value for a key using the built-in function delete
delete(m, "three")
_, ok = m["three"]
if ok {
fmt.Println("'three' is still a key in the map")
} else {
fmt.Println("'three' has been deleted from the map")
}
}
##############END OF SOURCE CODE#########################
Below is the explanation for the program:
In this program, we first declare a map of string keys and integer values using the map literal syntax and the syntax m := map[string]int{key1: value1, key2: value2, ...}.
We then retrieve the value for a key from the map using different approaches:
Using the index operator, which simply returns the value for the given key. We assign the value to a variable one and print it using the statement fmt.Println("Value for 'one':", one).
Using the built-in function make(), which returns the value for the given key and a boolean flag indicating whether the key exists in the map or not. We assign the value to a variable two and use the boolean flag to check if the key exists in the map. We print the value using the statement fmt.Println("Value for 'two':", two) if the key exists, or a message saying so if it does not.
Using the comma ok idiom, which assigns the value to a variable three and a boolean flag indicating whether the key exists in the map or not. We use the boolean flag to check if the key exists in the map. We print the value using the statement fmt.Println("Value for 'three':", three) if the key exists, or a message saying so if it does not.
Using the built-in function delete(), which removes the key-value pair from the map. We then use the boolean flag to check if the key exists in the map. We print a message saying that the key has been deleted from the map using the statement fmt.Println("'three' has been deleted from the map") if it does not exist in the map, or a message saying that it still exists using the statement fmt.Println("'three' is still a key in the map") if it does.
#go #goprogramming #golang #golangtutorial #golanguage