0

Came across the following differences a function implementations. What is the reasoning behind Example 1 returning a pointer and Example 2 returning an actual object?

type MyInterface interface {
    Func (param int) float64 //just random signature
}

//MyInterfaceImpl implements MyInterface
type MyInterfaceImpl struct {
}

//actual implementation 
func (myObj *MyInterfaceImpl) Func(param int) float64 {
    return float64(param)
}

Example 1: the pointer to the MyInterfaceImpl is returned when function returns an interface

func NewMyInterface() MyInterface {
    return &MyInterfaceImpl{}
}

Example 2: actual object of MyInterfaceImpl is returned when function returns the object

func NewMyInterfaceImpl() MyInterfaceImpl {
    return MyInterfaceImpl{}
}

UPDATE: This piece of code compiles and runs

func main() {
    myIf := NewMyInterface()
    fmt.Printf("Hi from inteface %f\n", myIf.Func(1000))

    myImpl := NewMyInterfaceImpl()
    fmt.Printf("Hi from impl %f\n", myImpl.Func(100))
}

UPDATE2: Question clarification.

This sounds weird (for me) to have a declaration of func NewMyInterface() MyInterface and a valid implementation of return &MyInterfaceImpl{} where a pointer is returned. I would expect to return an object of MyInterfaceImpl with return MyInterfaceImpl{}

If the language allows such types of constructs, there must be a reason for that. Eventually, I am looking for a following answer: "The function declaration returns an interface. Because the interface has a property X it is does not make sense to return an object, but the only valid option is a pointer".

Timofey
  • 2,478
  • 3
  • 37
  • 53
  • Are you sure its not `NewMyInterface() *MyInterface {` for the first example? – Ibu Mar 08 '19 at 22:02
  • @lbu Updated the description – Timofey Mar 08 '19 at 22:13
  • Possible duplicate of [Golang Pointer and Struct member function](https://stackoverflow.com/questions/40112250/golang-pointer-and-struct-member-function) – munk Mar 08 '19 at 22:21
  • Is the question about why `var myIf MyInterface = &MyInterfaceImpl{}` would be valid (as opposed to `var myIf *MyInterface = &MyInterfaceImpl{}`)? – P Varga Mar 08 '19 at 22:35
  • I think this is different from the pointer/value thing (which I also wrote about: https://stackoverflow.com/questions/23542989/pointers-vs-values-in-parameters-and-return-values/23551970). You return an interface if you don't necessarily want to expose everything about the type you're using internally, like because you might switch implementations. (For example, if you have something to return a cache or DB connection, you might wrap it in an interface that hides methods specific to the particular cache or DB driver being used.) You return the concrete type if you do want those details used. – twotwotwo Mar 08 '19 at 22:36
  • Or is the question about why `myImpl.Func()` works, even though `Func` has a pointer receiver and `myImpl` is not a pointer? – P Varga Mar 08 '19 at 22:48
  • 1
    `What is the reasoning behind Example 1 returning a pointer and Example 2 returning an actual object?` depends what they do, their members, how they are being used. With current examples its hard to tell. Now, as a rule of thumb, take interface, return structs. to read https://medium.com/@cep21/preemptive-interface-anti-pattern-in-go-54c18ac0668a –  Mar 08 '19 at 22:49
  • 1
    @mh-cbon *Example 1* **has to** return a pointer, as `MyInterfaceImpl` doesn't implement `MyInterface` (despite the name...) But I agree it's hard to tell what the question is. – P Varga Mar 08 '19 at 22:55
  • indeed, i totally missed that part. –  Mar 08 '19 at 22:57
  • I am confused about what you are asking. There are two functions that do different things. Depending on context, they can both be useful. Are you asking why NewMyInterface cannot return `MyInterfaceImpl{}`? – Charlie Tumahai Mar 08 '19 at 23:13
  • @ııı how do you tell that the ```MyInterfaceImpl``` does not implement ```MyInterface```? If it didn't, Example 1 cannot be compiled. @all I added the clarification – Timofey Mar 08 '19 at 23:14
  • @Tim See ThunderCat's explanation below, they were faster. Also if you remove the `&`, the compiler's error will explain why – P Varga Mar 08 '19 at 23:37

1 Answers1

1

Even though I'm not sure which part of the code the question is about, let me explain what the code does:

MyInterface is implemented by anything having a Func(int)float64 method.
*MyInterfaceImpl has such a method. However, MyInterfaceImpl does not (the method has a pointer receiver).

NewMyInterface() thus has to return a pointer. MyInterfaceImpl{} wouldn't implement MyInterface.

Does this answer your question?


Another question might be why the call myImpl.Func(100) works, despite the above. This is because Go automatically takes the address of the receiver when calling its methods with pointer receivers.
This is explained in more detail for example here.

P Varga
  • 19,174
  • 12
  • 70
  • 108
  • Could you please clarify why ```MyInterfaceImpl``` does not implement ```MyInterface```? It sounds like I am missing a fundamental go knowledge here. – Timofey Mar 08 '19 at 23:17
  • 1
    The [spec explains it](https://golang.org/ref/spec#Method_sets). The method set for a value type does not include pointer receiver methods. – Charlie Tumahai Mar 08 '19 at 23:18