Function Over Loading in GO using interfaces -


I have "main type" and it has "sub-type" embedded in it. Both implement the main and sub interface.

When I am specifying the 'main type' variable using the interface type variable and the call interface variable, this does not implement the "main type" implementation type. I have a sub-type implementation.

Is this possible? I think there is some problem in my code design here I am giving a sample code to describe this problem.

  Package main import "fmt" type interface interface {typeCheck ()} type maintainype struct {subtype} type subtype struct {} func (Maintype) type check () {fmt.Println ("Hi , It is printed in the main type ")} func (subtype) type check () {fmt.Println (" Hi, it is printed in sub type ")} func main () {var intr interf var maintp maintainype intr = maintp intr .typeCheck () // Out: "Hello, it is printed in the main type" I want "Hello, it is printed in subtype"}  

playground:

Please help ....

You need to explicitly specify the embedded subtype of your interface:

  func Main () {var intr interf var maintype intr = maintp.subtype // & lt; ==== intr.typeCheck ()}  

Output ( ):

  Hi, this is printed in subtype In the "Type Embedding and Implementation Inheritance" section, this article describes why "works". 
This solution is in "Interface through type replacement"

Completes type replacement via the use of the interface.
The interface allows us to define an abstract behavior and satisfy different types of behavior.

It still depends on affecting the correct subtype though for the interface variable.


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -