Golang how to do type assertion for unknown interface?

后端 未结 1 1196
醉梦人生
醉梦人生 2020-12-16 02:30

I understand that I can get an object\'s value by reflection and then use type assertion to get back the actual object using:

obj := new(User)
out := reflect         


        
相关标签:
1条回答
  • 2020-12-16 02:37

    You can't. Type assertions allow you to take advantage of the static type checking that the language gives you even if you have an interface, whose type isn't statically checked. It basically works something like this:

    You have some statically typed variable s, which has type t. The compiler enforces the guarantee that s always has type t by refusing to compile if you ever try to use s as if it were a different type, since that would break the guarantee.

    You also have some interface variable, i. i's type is not known at compile-time, so there's no way the compiler can guarantee that assigning i to s wouldn't break the guarantee that s had type t. However, what you can do is a type assertion. A type assertion side-steps this problem by saying, "well, I'll check at run-time, and I'll only do the assignment if the types match up." The compiler is OK with this because it knows that the assignment will only happen if the types match up, which means that it can still guarantee that s has type t. So basically what's happening at runtime is:

    if (i has type t) {
        s = i
    } else {
        s = t{} // Zero value of t
    }
    

    The reason that what you're asking for is impossible is that the compiler has to know what type you're checking against so that it can write the check that I gave pseudocode for above. Without knowing what t is, there's no way to know what the static type of s should be, and no way to check whether it's right.

    0 讨论(0)
提交回复
热议问题