How to create and use nested maps in Golang 1.20

Channel:
Subscribers:
5,740
Published on ● Video Link: https://www.youtube.com/watch?v=pGK0OGnX9JY



Category:
Tutorial
Duration: 2:12
5 views
0


In this video we are going to generate source code for the GoLang Program, How to create and use nested maps 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() {
// Create a nested map of string keys and int values
inventory := map[string]map[string]int{
"apples": {
"quantity": 50,
"price": 2,
},
"bananas": {
"quantity": 20,
"price": 1,
},
"oranges": {
"quantity": 30,
"price": 3,
},
}

// Access and modify the nested map
inventory["apples"]["quantity"] += 10
inventory["bananas"]["price"] = 2

// Iterate over the nested map
for name, item := range inventory {
fmt.Printf("Item: %s\n", name)
for key, value := range item {
fmt.Printf("%s: %d\n", key, value)
}
fmt.Println("--------------------")
}
}
##############END OF SOURCE CODE#########################

Below is the explanation for the program:

In this program, we create a nested map of string keys and int values called inventory.
The outer map has string keys that correspond to the names of different items,
and the values of the outer map are themselves maps with string keys and int values that correspond to the quantity and price of each item.

We then access and modify the nested map by incrementing the quantity of apples by 10 and changing the price of bananas to 2.

Finally, we use a nested loop to iterate over the keys and values of the nested map.
The outer loop iterates over the keys of the outer map, which are the names of the items.
For each item, the inner loop iterates over the keys and values of the inner map,
which are the quantity and price of the item.
The program prints the name of each item and its quantity and price, separated by dashes.

#go #goprogramming #golang #golangtutorial #golanguage







Tags:
how to create nested maps in golang
go program to use nested maps
nested maps in go
maps in go
go Programming Tutorial
go by example
go Language
go projects
go programming projects
go tutorial
golang tutorial
golang projects
go programming course
learn go
how to run go program
golang videos
golang with chatgpt
coding questions
coding interview questions