LoginSignup
0
0

More than 3 years have passed since last update.

【Golang】Switch文

Posted at

【Golang】Switch文

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

package main
//swich
//if swichの違いは?
//簡単な分岐処理ならswitchの方がすっきりする。
//型アサーションができる。

/*
まず、Goでは選択されたcaseだけを実行し、それに続く全てのcase節は実行されない。
他の言語では各case節の最後にbreakステートメントを記述しますが、Goでは自動的にbreakが入る。
*/

import (
    "fmt"
    "time"
)


func main(){
    //今の時間取得
    t := time.Now()
    //今の時間の時間だけ取得
    fmt.Println(t.Hour())

    //switch 分岐
    switch {
    case t.Hour() < 12:
        fmt.Println("午前")
    case t.Hour() > 12:
        fmt.Println("午後")
    }



    n := 1

    switch n {
    case 1:
        n += 1
        fmt.Println("3以下")
    case 2:
        n += 1
        fmt.Println("3以上")
    default:
        fmt.Println("default")
    }
    fmt.Println(n)
}
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