Golang interfaces best practices


The circle and rect struct types both implement the geometry interface so we can use instances of these structs as arguments to measure. measure ( r ) measure ( c ) } $ go run interfaces.go {3 4} 12 14 {5} 78.53981633974483 31.41592653589793 Chapter 7 Interfaces. An interface type is defined by a set of methods. A value of interface type can hold any value that implements those methods. Here is a refactored version of our earlier example. This time we made the greeting feature more generic by defining a function called Greet which takes a param of interface type Namer. These interfaces have many methods that define all the possible ways data could be accessed for a particular object. For example, the UserDs might define a function GetOneById(id uint32) (*User, error) Ds interfaces are implemented by a corresponding database-specific struct. For instance, UserDs might be implemented by MySqlUserDs. 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. Wouldn't it be great if you could learn faster? Learn about What are the best practices in Golang? and stay updated about the technologies and trends & show off your skills now!