0
0

More than 3 years have passed since last update.

golang 型switch記法

Posted at

golang勉強。
型switch記法というのが本に載ってたので、動くように書いてみた。
どの値を入れるとどう返すかをint型だけにして、main の中で直接書いてみた。

package main

import "fmt"

func main() {

        var x interface{} = 4  //この値をswitchのcaseで判定
        switch x := x.(type) {
        case int:
                fmt.Printf("%#v %T\n", x, x)
        }

        fmt.Println("Hello")
}

reflectを使ってtypeを判定する例

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var x float64 = 3.4
    fmt.Println("type:", reflect.TypeOf(x))
}

参考

Go言語の型とreflect

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