Go Interface: Go with duck types.
In computer programming, we are all familiar with duck typing, defined in Wikipedia as follows:
In computer programming, we are all familiar with duck typing, defined in Wikipedia as follows:
In computer programming, duck typing is an application of the duck test — “If it walks like a duck and it quacks like a duck, then it must be a duck” — to determine whether an object can be used for a particular purpose. With nominative typing, an object is of a given type if it is declared as such (or if a type’s association with the object is inferred through mechanisms such as object inheritance). With duck typing, an object is of a given type if it has all methods and properties required by that type. Duck typing may be viewed as a usage-based structural equivalence between a given object and the requirements of a type.
Duck Typing
Duck Typing, a concept in dynamic programming languages, emphasizes how objects can be used rather than their specific types. Go, as a statically typed language, elegantly supports duck typing through interfaces.
For instance, in the dynamic language Python, consider defining a function like this:
def hello_world(coder):
coder.say_hello()When calling this function, you can pass any type as long as it implements the say_hello() function. If it's not implemented, an error occurs during runtime. However, in statically typed languages like Java or C++, you must explicitly declare the implementation of an interface before using it anywhere. If you call the hello_world function with a type that doesn't implement say_hello(), it won't pass the compilation phase. This demonstrates the safety advantage of statically typed languages over dynamic ones.
The difference between dynamic and static languages becomes apparent here. Statically typed languages can catch type mismatches during compilation, unlike dynamic languages, which may encounter errors only when reaching the respective line of code. It’s worth mentioning, this is one reason why I’m not particularly fond of using Python. Of course, static languages require programmers to define data types for variables during coding, which, to some extent, increases workload and code length. Dynamic languages, on the other hand, allow focusing more on business logic, resulting in shorter and faster-to-write code, a point well understood by Python enthusiasts.
Go, being a modern statically typed language, offers the best of both worlds. It combines the convenience of dynamic languages with the type-checking of static ones, making coding a delightful experience. Go adopts a middle-ground approach: it doesn’t mandate explicit declaration of types implementing an interface; instead, as long as relevant methods are implemented, the compiler can detect it.
Example
Consider this example:
First, define an interface and a function that accepts this interface as a parameter:
type IGreeting interface {
sayHello()
}
func sayHello(i IGreeting) {
i.sayHello()
}Then, define two structs:
type Go struct {}
func (g Go) sayHello() {
fmt.Println("Hi, I am GO!")
}
type PHP struct {}
func (p PHP) sayHello() {
fmt.Println("Hi, I am PHP!")
}Finally, call the sayHello() function in the main function:
func main() {
golang := Go{}
php := PHP{}
sayHello(golang)
sayHello(php)
}The program output:
Hi, I am GO!
Hi, I am PHP!In the main function, when calling sayHello(), objects golang and php are passed. They haven't explicitly declared implementation for the IGreeting type; they merely implement the sayHello() function as specified by the interface. In reality, the compiler implicitly converts golang and php objects to the IGreeting type when calling sayHello(), showcasing the type-checking feature of statically typed languages.
A brief mention of the characteristics of dynamic languages:
The type of variable binding is uncertain until runtime. Functions and methods can accept parameters of any type without checking the parameter type. There is no need to implement interfaces.
summary
In summary, duck typing is a style of dynamic language where an object’s valid semantics are determined not by inheriting from specific classes or implementing specific interfaces but by its “current set of methods and properties.” Go, as a statically typed language, achieves duck typing through interfaces, where the Go compiler performs implicit conversions.