Acessando Valores do Mapa
Podemos acessar o valor correspondente a uma chave em um mapa usando a seguinte sintaxe:
index.go
1mapName["keyName"]
Please provide the text you want to be translated into Portuguese, and I will be happy to assist with the translation.
index.go
1234567891011121314package 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 }
A expressão prices["apple"]
refere-se basicamente ao local de memória onde o valor 100
está armazenado; portanto, ela funciona como uma variável. Dessa forma, podemos editar o valor guardado nessa chave utilizando o operador =
:
index.go
12345678910111213package main import "fmt" func main() { prices := map[string]int { "apple": 100, "banana": 120, "peach": 170, } prices["apple"] = 160 fmt.Println(prices["apple"]) // Output: 160 }
Podemos utilizar a mesma sintaxe de atribuição para criar uma nova chave no mapa:
mapName["keyName"] = value
Se a keyName
fornecida não existir no mapa, ela criará e adicionará uma nova chave ao mapa com o valor atribuído.
index.go
1234567891011121314151617package 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] }
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
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
Acessando Valores do Mapa
Deslize para mostrar o menu
Podemos acessar o valor correspondente a uma chave em um mapa usando a seguinte sintaxe:
index.go
1mapName["keyName"]
Please provide the text you want to be translated into Portuguese, and I will be happy to assist with the translation.
index.go
1234567891011121314package 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 }
A expressão prices["apple"]
refere-se basicamente ao local de memória onde o valor 100
está armazenado; portanto, ela funciona como uma variável. Dessa forma, podemos editar o valor guardado nessa chave utilizando o operador =
:
index.go
12345678910111213package main import "fmt" func main() { prices := map[string]int { "apple": 100, "banana": 120, "peach": 170, } prices["apple"] = 160 fmt.Println(prices["apple"]) // Output: 160 }
Podemos utilizar a mesma sintaxe de atribuição para criar uma nova chave no mapa:
mapName["keyName"] = value
Se a keyName
fornecida não existir no mapa, ela criará e adicionará uma nova chave ao mapa com o valor atribuído.
index.go
1234567891011121314151617package 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] }
Obrigado pelo seu feedback!