This is the fifth day of my participation in Gwen Challenge

Why doesn’t Go support overloaded methods and operators?

Method scheduling is easy if you don’t need to do type matching. Experience in other languages has shown that while a method with the same name and a different function signature implementation can occasionally be helpful, it can also be confusing to use. Matching methods by name and ensuring type consistency was an important decision in the simplicity of the Go language design. The same is true for operator overloading.

Why doesn’t Go declare the implementation of the interface?

To implement an interface, you need to implement the methods in that interface, and that’s it. This feature allows you to define and use interfaces without modifying existing code. This design is similar to “structured type design” in that it separates concerns, improves code reusability, and allows you to quickly build code on top of a template. Interface design is one of the key reasons GO is agile and lightweight.

Nimble nimble nimble nimble nimble nimble nimble nimble nimble nimbleCopy the code

How do I make sure my interface really implements an interface?

You can use the compiler to check whether T implements interface I by using either a null value or a null pointer:

// Verify that T implements I.  
// (PS: the interface can accept its implementation, if not, it does not implement the corresponding interface)
// The following type will not return an error
// If T's find method is removed, an error is reported
type T struct{}func (t T) find(a){}type I interface{
	find()
}

func testSatisfy(a){
	var i I
	i = T{}
    fmt.Println(i)
}
Copy the code

If you want users of an interface to explicitly declare that they implement the interface, you can add a descriptive method to the interface. This method can be empty, but just for the sake of expression, it implements the interface. As follows:

type Fooer interface {
    Foo()
    ImplementsFooer()
}
Copy the code

So, a type must implement the ImplementsFooer method. Similar to the following:

type Bar struct{}
func (b Bar) ImplementsFooer(a) {}
func (b Bar) Foo(a) {}
Copy the code

Most code doesn’t use this because it breaks the conciseness of the interface, but it’s necessary for similar, confusing interfaces. (PS: not recommended)