1
2

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 3 years have passed since last update.

【Golang】for文

Posted at

#【Golang】for文
Golangの基礎学習〜Webアプリケーション作成までの学習を終えたので、復習を兼ねてまとめていく。 基礎〜応用まで。

package main
//for

import "fmt"


func main() {
	//パターン1
	//for 初期化、 条件、 繰り返し式
    for i := 0; i < 100; i++{
        if i == 3 {
			fmt.Println(3)
			//繰り返し forの中に戻る
			continue
		}
		if i > 30 {
			fmt.Println("抜けた")
			//強制終了
			break
		}
		fmt.Println("30まで")
	}
	//パターン2
	//書き方、別パターン
	sum := 1
	for sum < 10 {
		sum += sum
		fmt.Println(sum)
	}
	fmt.Println(sum)

	//無限ループ
	/*for {
		fmt.Println("hello")
	}*/





	//range
	//配列、スライス、マップ
	/*arr := [3]int{1,2,3}
	slise := []int{4,5,6}
	map1 := map[string]int{"AAA":1, "BBB":2}
	fmt.Println(arr)
	fmt.Println(slise)
	fmt.Println(map1)

	for i := range arr {
		fmt.Println(arr[i])
	}*/

	//スライス
	l := []string{"python", "go", "java"}
	
	//中身を取り出す。長さ分になるまで
	for i := 0; i < len(l); i++ {
		fmt.Println(i, l[i])
	}
	/*
	0 python
	1 go
	2 java
	*/
   
	//長さ分繰り返す
	for i, v := range l {
		fmt.Println(i, v)
	}
	/*
	0 python
	1 go
	2 java
	*/
	
	//index省略
	for _, v := range l {
		fmt.Println(v)
	}
	/*
	python
	go
	java
	*/
	
	//map
	m := map[string]int{"apple": 100, "banana": 200}
	
	//map分繰り返す
	for k, v := range m {
		fmt.Println(k, v)
	}
	/*
	apple 100
	banana 200
	*/
	
	//値省略
	for k := range m {
		fmt.Println(k)
	}
	/*
	banana
	apple
	*/
	
	//key省略
	for _, v := range m {
		fmt.Println(v)
	}
	/*
	100
    200
	*/
}

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?