How to determine an interface{} value's “real” type?

前端 未结 7 1239
青春惊慌失措
青春惊慌失措 2020-12-22 17:15

I have not found a good resource for using interface{} types. For example

package main

import \"fmt\"

func weirdFunc(i int) interface{} {
             


        
7条回答
  •  伪装坚强ぢ
    2020-12-22 17:31

    Type switches can also be used with reflection stuff:

    var str = "hello!"
    var obj = reflect.ValueOf(&str)
    
    switch obj.Elem().Interface().(type) {
    case string:
        log.Println("obj contains a pointer to a string")
    default:
        log.Println("obj contains something else")
    }
    

提交回复
热议问题