久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      golang循環(huán)遍歷map的方式有幾種

      golang循環(huán)遍歷map的方式有1種,就是利用“for range”語句。“for range”是Go語言特有的一種的迭代結構,語法“for key, value := range mapName{…}”,返回的第一個是map的key,返回的第二個是map的value;如果只使用一個返回參數(shù)接受,那么返回的是map的key。

      golang循環(huán)遍歷map的方式有幾種

      本教程操作環(huán)境:windows7系統(tǒng)、GO 1.18版本、Dell G3電腦。

      Go 語言 中 map 的遍歷只能使用 for range 的形式,for range 循環(huán)返回的第一個是 map 的 key,返回的第二個是 map 的 value。

      使用 for range 遍歷 map,如果我們只使用一個返回參數(shù)接受,那么返回的是 map 的 key。因此 map 是無序的,因此同一個 map,每次遍歷獲取的結果的順序很可能是不一致的。

      for range循環(huán)遍歷map語法

      • 遍歷map的key和value

      for key, value := range mapName{ ... }
      登錄后復制

      使用 for range 循環(huán)遍歷 變量 名為 mapName 的 map ,其中 key 是 map 的鍵,value 是 map 的 key 所對應的 值。

      • 遍歷map的key

      for key := range mapName{ ... }
      登錄后復制

      使用 for range 循環(huán)遍歷map 時 ,如果只使用一個返回值接受,那么返回的是 map 的 key。

      • 遍歷map的value

      for _, value := range mapName{ ... }
      登錄后復制

      如果我們僅僅希望獲取 map 的所有的元素,那么我們可以使用 _ 忽略 key 的值;將不需要的鍵使用_改為匿名變量形式。。

      for range循環(huán)遍歷map的示例

      示例1:使用 for range 循環(huán)遍歷 map,獲取 map 的 KEY 和 VALUE

      package main import ( 	"fmt" ) func main() { 	//使用 for range 循環(huán)遍歷 map,獲取 map 的 KEY 和 VALUE 	mapHaiCoder := map[string]string{ 		"Server":"Golang", 		"JavaScript":"Vue", 		"Db":"Redis", 	} 	for key, value := range mapHaiCoder{ 		fmt.Println("Key =", key, "Value =", value) 	} }
      登錄后復制

      golang循環(huán)遍歷map的方式有幾種

      示例2:使用 for range 循環(huán)遍歷 map 的所有的 key

      package main import ( 	"fmt" ) func main() { 	//使用 for range 循環(huán)遍歷 map,獲取 map 的 KEY 和 VALUE 	mapHaiCoder := map[string]string{ 		"Server":"Golang", 		"JavaScript":"Vue", 		"Db":"Redis", 	} 	for key := range mapHaiCoder{ 		fmt.Println("Key =", key, "Value =", mapHaiCoder[key]) 	} }
      登錄后復制

      golang循環(huán)遍歷map的方式有幾種

      示例3:遍歷map的value

      如果我們僅僅希望獲取 map 的所有的元素,那么我們可以使用 _ 忽略 key 的值

      package main import ( 	"fmt" ) func main() { 	//使用 for range 循環(huán)遍歷 map,獲取 map 的 KEY 和 VALUE 	mapHaiCoder := map[string]string{ 		"Server":"Golang", 		"JavaScript":"Vue", 		"Db":"Redis", 	} 	for _, value := range mapHaiCoder{ 		fmt.Println("Value =", value) 	} }
      登錄后復制

      golang循環(huán)遍歷map的方式有幾種

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號