官术网_书友最值得收藏!

  • 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)
}
}
主站蜘蛛池模板: 青冈县| 卢氏县| 克山县| 拜泉县| 郸城县| 华蓥市| 武夷山市| 炉霍县| 汉沽区| 龙口市| 钦州市| 措美县| 永仁县| 康平县| 泗洪县| 朝阳市| 上饶县| 盱眙县| 勃利县| 十堰市| 桐庐县| 同德县| 麟游县| 阳东县| 山西省| 噶尔县| 文登市| 绥中县| 图木舒克市| 康平县| 安龙县| 滕州市| 马龙县| 朝阳市| 广宁县| 安泽县| 辽宁省| 民丰县| 平顶山市| 阜南县| 九寨沟县|