Difference in a function implementation when instantiating an object with different function signaturesPointers vs. values in parameters and return valuesGolang Pointer and Struct member functionStack vs heap allocation of structs in Go, and how they relate to garbage collectionWhy does the compiler require such a strict match for function signatures?When is the init() function run?Why does putting a pointer in an interface in Go cause reflect to lose the name of the type?Implicit interface conversion in golangReturn different specialised implementations of interfaces from the same function in Go langfunction type parameter match in golangGolang change type of pointer interfaceStrange behaviour when Unmarshalling into struct in GoCall function of specific type in Go

How to draw the figure with four pentagons?

Could gravitational lensing be used to protect a spaceship from a laser?

Why does Arabsat 6A need a Falcon Heavy to launch

prove that the matrix A is diagonalizable

Can a rocket refuel on Mars from water?

Neighboring nodes in the network

Doing something right before you need it - expression for this?

Is delete *p an alternative to delete [] p?

Blender 2.8 I can't see vertices, edges or faces in edit mode

I would say: "You are another teacher", but she is a woman and I am a man

How to model explosives?

Do I have a twin with permutated remainders?

If a Gelatinous Cube takes up the entire space of a Pit Trap, what happens when a creature falls into the trap but succeeds on the saving throw?

How to prevent "they're falling in love" trope

Fully-Firstable Anagram Sets

Arrow those variables!

In a Spin are Both Wings Stalled?

Emailing HOD to enhance faculty application

Is Lorentz symmetry broken if SUSY is broken?

Where does SFDX store details about scratch orgs?

What is the PIE reconstruction for word-initial alpha with rough breathing?

Why do I get two different answers for this counting problem?

Combinations of multiple lists

How much of data wrangling is a data scientist's job?



Difference in a function implementation when instantiating an object with different function signatures


Pointers vs. values in parameters and return valuesGolang Pointer and Struct member functionStack vs heap allocation of structs in Go, and how they relate to garbage collectionWhy does the compiler require such a strict match for function signatures?When is the init() function run?Why does putting a pointer in an interface in Go cause reflect to lose the name of the type?Implicit interface conversion in golangReturn different specialised implementations of interfaces from the same function in Go langfunction type parameter match in golangGolang change type of pointer interfaceStrange behaviour when Unmarshalling into struct in GoCall function of specific type in Go






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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 %fn", myIf.Func(1000))

myImpl := NewMyInterfaceImpl()
fmt.Printf("Hi from impl %fn", 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".










share|improve this question
























  • Are you sure its not NewMyInterface() *MyInterface { for the first example?

    – Ibu
    Mar 8 at 22:02











  • @lbu Updated the description

    – Timofey
    Mar 8 at 22:13












  • Possible duplicate of Golang Pointer and Struct member function

    – munk
    Mar 8 at 22:21






  • 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 medium.com/@cep21/…

    – mh-cbon
    Mar 8 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.

    – ᆼᆺᆼ
    Mar 8 at 22:55


















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 %fn", myIf.Func(1000))

myImpl := NewMyInterfaceImpl()
fmt.Printf("Hi from impl %fn", 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".










share|improve this question
























  • Are you sure its not NewMyInterface() *MyInterface { for the first example?

    – Ibu
    Mar 8 at 22:02











  • @lbu Updated the description

    – Timofey
    Mar 8 at 22:13












  • Possible duplicate of Golang Pointer and Struct member function

    – munk
    Mar 8 at 22:21






  • 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 medium.com/@cep21/…

    – mh-cbon
    Mar 8 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.

    – ᆼᆺᆼ
    Mar 8 at 22:55














0












0








0


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 %fn", myIf.Func(1000))

myImpl := NewMyInterfaceImpl()
fmt.Printf("Hi from impl %fn", 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".










share|improve this question
















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 %fn", myIf.Func(1000))

myImpl := NewMyInterfaceImpl()
fmt.Printf("Hi from impl %fn", 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".







go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 23:10







Timofey

















asked Mar 8 at 21:38









TimofeyTimofey

1,61923143




1,61923143












  • Are you sure its not NewMyInterface() *MyInterface { for the first example?

    – Ibu
    Mar 8 at 22:02











  • @lbu Updated the description

    – Timofey
    Mar 8 at 22:13












  • Possible duplicate of Golang Pointer and Struct member function

    – munk
    Mar 8 at 22:21






  • 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 medium.com/@cep21/…

    – mh-cbon
    Mar 8 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.

    – ᆼᆺᆼ
    Mar 8 at 22:55


















  • Are you sure its not NewMyInterface() *MyInterface { for the first example?

    – Ibu
    Mar 8 at 22:02











  • @lbu Updated the description

    – Timofey
    Mar 8 at 22:13












  • Possible duplicate of Golang Pointer and Struct member function

    – munk
    Mar 8 at 22:21






  • 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 medium.com/@cep21/…

    – mh-cbon
    Mar 8 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.

    – ᆼᆺᆼ
    Mar 8 at 22:55

















Are you sure its not NewMyInterface() *MyInterface { for the first example?

– Ibu
Mar 8 at 22:02





Are you sure its not NewMyInterface() *MyInterface { for the first example?

– Ibu
Mar 8 at 22:02













@lbu Updated the description

– Timofey
Mar 8 at 22:13






@lbu Updated the description

– Timofey
Mar 8 at 22:13














Possible duplicate of Golang Pointer and Struct member function

– munk
Mar 8 at 22:21





Possible duplicate of Golang Pointer and Struct member function

– munk
Mar 8 at 22:21




1




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 medium.com/@cep21/…

– mh-cbon
Mar 8 at 22:49





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 medium.com/@cep21/…

– mh-cbon
Mar 8 at 22:49




1




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.

– ᆼᆺᆼ
Mar 8 at 22:55






@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.

– ᆼᆺᆼ
Mar 8 at 22:55













1 Answer
1






active

oldest

votes


















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.






share|improve this answer

























  • Could you please clarify why MyInterfaceImpl does not implement MyInterface? It sounds like I am missing a fundamental go knowledge here.

    – Timofey
    Mar 8 at 23:17






  • 1





    The spec explains it. The method set for a value type does not include pointer receiver methods.

    – Cerise Limón
    Mar 8 at 23:18











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071358%2fdifference-in-a-function-implementation-when-instantiating-an-object-with-differ%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer

























  • Could you please clarify why MyInterfaceImpl does not implement MyInterface? It sounds like I am missing a fundamental go knowledge here.

    – Timofey
    Mar 8 at 23:17






  • 1





    The spec explains it. The method set for a value type does not include pointer receiver methods.

    – Cerise Limón
    Mar 8 at 23:18















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.






share|improve this answer

























  • Could you please clarify why MyInterfaceImpl does not implement MyInterface? It sounds like I am missing a fundamental go knowledge here.

    – Timofey
    Mar 8 at 23:17






  • 1





    The spec explains it. The method set for a value type does not include pointer receiver methods.

    – Cerise Limón
    Mar 8 at 23:18













1












1








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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 23:31

























answered Mar 8 at 23:09









ᆼᆺᆼᆼᆺᆼ

8,67943563




8,67943563












  • Could you please clarify why MyInterfaceImpl does not implement MyInterface? It sounds like I am missing a fundamental go knowledge here.

    – Timofey
    Mar 8 at 23:17






  • 1





    The spec explains it. The method set for a value type does not include pointer receiver methods.

    – Cerise Limón
    Mar 8 at 23:18

















  • Could you please clarify why MyInterfaceImpl does not implement MyInterface? It sounds like I am missing a fundamental go knowledge here.

    – Timofey
    Mar 8 at 23:17






  • 1





    The spec explains it. The method set for a value type does not include pointer receiver methods.

    – Cerise Limón
    Mar 8 at 23:18
















Could you please clarify why MyInterfaceImpl does not implement MyInterface? It sounds like I am missing a fundamental go knowledge here.

– Timofey
Mar 8 at 23:17





Could you please clarify why MyInterfaceImpl does not implement MyInterface? It sounds like I am missing a fundamental go knowledge here.

– Timofey
Mar 8 at 23:17




1




1





The spec explains it. The method set for a value type does not include pointer receiver methods.

– Cerise Limón
Mar 8 at 23:18





The spec explains it. The method set for a value type does not include pointer receiver methods.

– Cerise Limón
Mar 8 at 23:18



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071358%2fdifference-in-a-function-implementation-when-instantiating-an-object-with-differ%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived