4 ms·
Go has so called type assertions, an example: http://play.golang.org/p/L5hprjhqN_ http://play.golang.org/p/L5hprjhqN_ In this example, one function probes the
by kitsune_ 13y ago
Go has so called type assertions, an example:
http://play.golang.org/p/L5hprjhqN_ http://play.golang.org/p/L5hprjhqN_
In this example, one function probes the underlying type by using t.(type), and the other one uses a type assertion by doing a val, ok := t.(int) - The type assertion will return the value of the integer and true if t happens to be an integer (a Go function can return multiple values)
If you pass an object as an "empty interface{}" you can test the underlying type.
edit: Note that you can only use a type assertion with an interface! So, this fails: http://play.golang.org/p/0BtSWfE3f2 http://play.golang.org/p/0BtSWfE3f2 but this doesn't: http://play.golang.org/p/tZB4Jivp3z http://play.golang.org/p/tZB4Jivp3z
- lloeki 13y agoIs this purely evaluated at runtime or does Go annotates types with its static analysis and restricts the interface in some ways? IOW are default and else cases made optional by Go's static analysis, which would help Go throw its arms up at compile time if an unexpected type is passed?
- kitsune_ 13y agoI think it's purely evaluated at run-time because you can do this: http://play.golang.org/p/Mtg8A5SzdS http://play.golang.org/p/Mtg8A5SzdS
- chimeracoder 13y ago> Is this purely evaluated at runtime or does Go annotates types with its static analysis and restricts the interface in some ways? Interfaces in a nutshell: Go checks at compile-time that any concrete variable passed where in interface is expected satisfies the methods of that interface. Conversely, Go checks at compile-time that the program never attempts to invoke methods on an interface value that are not guaranteed by the interface. In this example, there is a compile-time error (not a runtime error), because we attempt to call a non-guaranteed method on an interface value, even though the underlying value satisfies this interface: http://play.golang.org/p/EaQQpv-NAW http://play.golang.org/p/EaQQpv-NAW This provides type safety: if your program compiles, you know that you will never run into a runtime error by trying to invoke an undefined method. When you do type assertions, you're using reflection (ie, http://golang.org/pkg/reflect/ http://golang.org/pkg/reflect/) to determine the underlying value at runtime. However, remember that using type assertions essentially sidesteps the benefits of having interfaces. Interfaces allow you to invoke function calls with type safety on values of unknown type, as long as it is known that the underlying type provides the required set of methods.
- danieldk 13y agoIf you pass an object as an "empty interface{}" you can test the underlying type. So, the answer is that it is pretty much the same as in Java, since there you also use reflection to get the type of Object: if (o instanceof String) {...} The type assertion is just syntactic sugar. The difference is that Java stores the type in the actual instance, while Go stores the type in the 'reference' (which is normally a type-pointer tuple). Unfortunately, Go's approach can lead to the situation where nil (null) is not nil, when two references have null as its pointer, but different types.