0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Golang]マップやスライスをループさせるがキーやインデックスが必要でない場合、ブランク修飾子が便利な件

Last updated at Posted at 2019-08-11

とりあえずサンプルコード

スライスの場合

スライスの場合
package main
import "fmt"

func main() {
	s := []int{2, 4, 6, 8}
	for _, v := range s {
		fmt.Println(v)
	}
}
スライスの場合 実行結果
2
4
6
8

マップの場合

マップの場合
package main
import "fmt"

func main() {
	m := map[string]int{"maeda":165, "yamada":175, "suzuki":180}
	for _, v := range m {
		fmt.Println(v)
	}
}
マップの場合 実行結果
165
175
180

ブランク修飾子の書き方

変数名のところに_このように、アンダースコアを書くだけです。
こうすることで、キーやインデックスに入れられる予定だった値を、処理の中で使わなくても、怒られません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?