Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Accessing Map Values | Intro to Structs & Maps
Introduktion till Golang

bookAccessing Map Values

We can access the value corresponding to a key in a map using the following syntax:

index.go

index.go

copy
1
mapName["keyName"]

For example:

index.go

index.go

copy
1234567891011121314
package main import "fmt" func main() { prices := map[string]int { "apple": 100, "banana": 120, "peach": 170, } fmt.Println(prices["apple"]) // Output: 100 fmt.Println(prices["banana"]) // Output: 120 fmt.Println(prices["peach"]) // Output: 170 }

The expression prices["apple"] essentially references the memory location where the value 100 is stored; hence, it acts like a variable. Therefore, we can edit the value stored at that key using the = operator:

index.go

index.go

copy
12345678910111213
package main import "fmt" func main() { prices := map[string]int { "apple": 100, "banana": 120, "peach": 170, } prices["apple"] = 160 fmt.Println(prices["apple"]) // Output: 160 }

We can use the same assignment syntax to create a new key in the map:

mapName["keyName"] = value

If the provided keyName doesn't exist in the map, it will create and add a new key to the map with the assigned value.

index.go

index.go

copy
1234567891011121314151617
package main import "fmt" func main() { numbers := map[string]int { "one": 1, "two": 2, "three": 3, } fmt.Println(numbers) // Output: map[one:1 three:3 two:2] numbers["four"] = 4 fmt.Println(numbers) // Output: map[four:4 one:1 three:3 two:2] }
question mark

What will be the output of the following code?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you show an example of updating a value in a map?

How do I check if a key exists before updating it?

What happens if I assign a value to an existing key?

Awesome!

Completion rate improved to 1.96

bookAccessing Map Values

Svep för att visa menyn

We can access the value corresponding to a key in a map using the following syntax:

index.go

index.go

copy
1
mapName["keyName"]

For example:

index.go

index.go

copy
1234567891011121314
package main import "fmt" func main() { prices := map[string]int { "apple": 100, "banana": 120, "peach": 170, } fmt.Println(prices["apple"]) // Output: 100 fmt.Println(prices["banana"]) // Output: 120 fmt.Println(prices["peach"]) // Output: 170 }

The expression prices["apple"] essentially references the memory location where the value 100 is stored; hence, it acts like a variable. Therefore, we can edit the value stored at that key using the = operator:

index.go

index.go

copy
12345678910111213
package main import "fmt" func main() { prices := map[string]int { "apple": 100, "banana": 120, "peach": 170, } prices["apple"] = 160 fmt.Println(prices["apple"]) // Output: 160 }

We can use the same assignment syntax to create a new key in the map:

mapName["keyName"] = value

If the provided keyName doesn't exist in the map, it will create and add a new key to the map with the assigned value.

index.go

index.go

copy
1234567891011121314151617
package main import "fmt" func main() { numbers := map[string]int { "one": 1, "two": 2, "three": 3, } fmt.Println(numbers) // Output: map[one:1 three:3 two:2] numbers["four"] = 4 fmt.Println(numbers) // Output: map[four:4 one:1 three:3 two:2] }
question mark

What will be the output of the following code?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 6
some-alt