Difference in a function implementation when instantiating an object with different function signatures Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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
What would you call this weird metallic apparatus that allows you to lift people?
What are the discoveries that have been possible with the rejection of positivism?
Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?
How to identify unknown coordinate type and convert to lat/lon?
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
Why can't I install Tomboy in Ubuntu Mate 19.04?
Why weren't discrete x86 CPUs ever used in game hardware?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
Semigroups with no morphisms between them
What's the point of the test set?
Why are my pictures showing a dark band on one edge?
Lagrange four-squares theorem --- deterministic complexity
Amount of permutations on an NxNxN Rubik's Cube
How to write capital alpha?
Google .dev domain strangely redirects to https
How many morphisms from 1 to 1+1 can there be?
What is the meaning of 'breadth' in breadth first search?
Tannaka duality for semisimple groups
In musical terms, what properties are varied by the human voice to produce different words / syllables?
What order were files/directories output in dir?
If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?
How to compare two different files line by line in unix?
preposition before coffee
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
Difference in a function implementation when instantiating an object with different function signatures
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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;
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
|
show 7 more comments
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
Are you sure its notNewMyInterface() *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, asMyInterfaceImpl
doesn't implementMyInterface
(despite the name...) But I agree it's hard to tell what the question is.
– ᆼᆺᆼ
Mar 8 at 22:55
|
show 7 more comments
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
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
go
edited Mar 8 at 23:10
Timofey
asked Mar 8 at 21:38
TimofeyTimofey
1,62923143
1,62923143
Are you sure its notNewMyInterface() *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, asMyInterfaceImpl
doesn't implementMyInterface
(despite the name...) But I agree it's hard to tell what the question is.
– ᆼᆺᆼ
Mar 8 at 22:55
|
show 7 more comments
Are you sure its notNewMyInterface() *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, asMyInterfaceImpl
doesn't implementMyInterface
(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
|
show 7 more comments
1 Answer
1
active
oldest
votes
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.
Could you please clarify whyMyInterfaceImpl
does not implementMyInterface
? 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Could you please clarify whyMyInterfaceImpl
does not implementMyInterface
? 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
add a comment |
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.
Could you please clarify whyMyInterfaceImpl
does not implementMyInterface
? 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
add a comment |
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.
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.
edited Mar 8 at 23:31
answered Mar 8 at 23:09
ᆼᆺᆼᆼᆺᆼ
8,73243564
8,73243564
Could you please clarify whyMyInterfaceImpl
does not implementMyInterface
? 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
add a comment |
Could you please clarify whyMyInterfaceImpl
does not implementMyInterface
? 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 implementMyInterface
(despite the name...) But I agree it's hard to tell what the question is.– ᆼᆺᆼ
Mar 8 at 22:55