- Security with Go
- John Daniel Leon
- 130字
- 2021-06-30 19:06:49
range
The range keyword is used to iterate over a slice, map, or other data structure. The range keyword is used in combination with the for loop, to operate on an iterable data structure. The range keyword returns the key and value variables. Here are some basic examples of using the range keyword:
package main
import "fmt"
func main() {
intSlice := []int{2, 4, 6, 8}
for key, value := range intSlice {
fmt.Println(key, value)
}
myMap := map[string]string{
"d": "Donut",
"o": "Operator",
}
// Iterate over a map
for key, value := range myMap {
fmt.Println(key, value)
}
// Iterate but only utilize keys
for key := range myMap {
fmt.Println(key)
}
// Use underscore to ignore keys
for _, value := range myMap {
fmt.Println(value)
}
}
推薦閱讀
- 一定要懂博弈論
- 妙趣橫生博弈論:事業(yè)與人生的成功之道(白金版)
- 魔方的思維世界
- 數(shù)獨(dú)游戲全集
- 排序問題的數(shù)學(xué)規(guī)劃松弛方法
- 趣味魔方:一學(xué)就會(huì)的魔方秘笈
- 愛情數(shù)學(xué)(TED 思想的力量系列)
- 2頁紙圖解數(shù)學(xué):以極聰明的方式,讓你三步讀懂?dāng)?shù)學(xué)
- 特殊函數(shù)概論習(xí)題解答
- 基于變分法的細(xì)胞演化建模
- 高等數(shù)學(xué)習(xí)題全解與學(xué)習(xí)指導(dǎo)(下冊(cè))
- 現(xiàn)代啟發(fā)式優(yōu)化方法及其應(yīng)用
- 給孩子的數(shù)學(xué)思維課
- 萊布尼茲微積分
- Radioss 基礎(chǔ)理論與工程高級(jí)應(yīng)用