HN2new | past | comments | ask | show | jobs | submitlogin

Is 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?


> 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

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/) 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.


I think it's purely evaluated at run-time because you can do this:

http://play.golang.org/p/Mtg8A5SzdS




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: