방법1. switch 를 활용한 방법
[code]
package main
import “fmt”
func main() {
var i interface{} = 5
switch t := i.(type) {
case int:
fmt.Println(“Type is an Integer: “, t)
case bool:
fmt.Println(“Type is a Boolean: “, t)
case string:
fmt.Println(“Type is a String: “, t)
}
}
[/code]
방법2. reflect 를 이용한 방법
[code]
package main
import (
“fmt”
“reflect”
)
func main() {
var i interface{} = 5
t := reflect.TypeOf(i).Kind()
if t == reflect.Int {
fmt.Println(“Type is an Integer: “, t)
} else if t == reflect.Bool {
fmt.Println(“Type is a Boolean: “, t)
} else if t == reflect.String {
fmt.Println(“Type is a String: “, t)
}
}
[/code]
방법3. Assert 를 이용한 방법
[code]
package main
import (
“fmt”
)
func main() {
var i interface{} = 5
if t, ok := i.(int); ok {
fmt.Println(“Type is an Integer: “, t)
} else if t, ok := i.(int); ok {
fmt.Println(“Type is a Boolean: “, t)
} else if t, ok := i.(int); ok {
fmt.Println(“Type is a String: “, t)
}
}
[/code]