Golang cast interface to struct


The first pointer will point to the underlying data (so it will be the same for both), but the second pointer points to an interface-specific itable struct that has (at fixed offsets specific to the interface type) the methods that the interface supports (along with type information that's used to perform casting to concrete types or to other ... Stringers. One of the most ubiquitous interfaces is Stringer defined by the fmt package. type Stringer interface { String() string } A Stringer is a type that can describe itself as a string. The fmt package (and many others) look for this interface to print values. < Type assertions. A type assertion provides access to an interface value's underlying concrete value. t := i.(T) This statement asserts that the interface value i holds the concrete type T and assigns the underlying T value to the variable t. If i does not hold a T, the statement will trigger a panic. Golang - Understand Structs and Interfaces. GitHub Gist: instantly share code, notes, and snippets. So now if I have to add any method for Chile, should be also added to interface or only parent's methods are added to interface? Re: How to type cast a child struct to parent type to pass to a func Haddock