Is there any difference between struct embedding and defining a field with the struct you want to embed?

So I’m looking at this article on Structure Embedding in GoLang.
When reading, I came upon this: “Note that the access co.b is a syntactic convenience; we can also do it more explicitly with co.Base.b.”.

So if that’s just a “syntactic convenience”, is…


This content originally appeared on DEV Community and was authored by Calin Baenen

So I'm looking at this article on Structure Embedding in GoLang.
When reading, I came upon this: "Note that the access co.b is a syntactic convenience; we can also do it more explicitly with co.Base.b.".

So if that's just a "syntactic convenience", is structure embedding equal to just adding a field?
E.g.

type A struct {
  Msg string;
}
func (this *A) Print() {
  fmt.Println("A: "+this.Msg);
}

type B struct {
  A;
}
func (this *B) Print() {
  fmt.Println("B: "+this.Msg);
}

==

type A struct {
  Msg string;
}
func (this *A) Print() {
  fmt.Println("A: "+this.Msg);
}

type B struct {
  A A;
}
func (this *B) Print() {
  fmt.Println("B: "+this.A.Msg);
}



Putting:

package main;

import "fmt";



type A struct {
  Msg string;
}
func (this *A) Print() {
  fmt.Println("A: "+this.Msg);
}

type B struct {
  A;
}
func (this *B) Print() {
  fmt.Println("B: "+this.Msg);
}

func main() {
  var test B = B{};
  test.Msg = "Hello!";

  test.A.Print();
  test.Print();
}

into SoloLearn's Go playground that seems to be the case.


So is there any difference, or am I right about them being the exact same thing?


This content originally appeared on DEV Community and was authored by Calin Baenen


Print Share Comment Cite Upload Translate Updates
APA

Calin Baenen | Sciencx (2021-05-24T16:09:35+00:00) Is there any difference between struct embedding and defining a field with the struct you want to embed?. Retrieved from https://www.scien.cx/2021/05/24/is-there-any-difference-between-struct-embedding-and-defining-a-field-with-the-struct-you-want-to-embed/

MLA
" » Is there any difference between struct embedding and defining a field with the struct you want to embed?." Calin Baenen | Sciencx - Monday May 24, 2021, https://www.scien.cx/2021/05/24/is-there-any-difference-between-struct-embedding-and-defining-a-field-with-the-struct-you-want-to-embed/
HARVARD
Calin Baenen | Sciencx Monday May 24, 2021 » Is there any difference between struct embedding and defining a field with the struct you want to embed?., viewed ,<https://www.scien.cx/2021/05/24/is-there-any-difference-between-struct-embedding-and-defining-a-field-with-the-struct-you-want-to-embed/>
VANCOUVER
Calin Baenen | Sciencx - » Is there any difference between struct embedding and defining a field with the struct you want to embed?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/24/is-there-any-difference-between-struct-embedding-and-defining-a-field-with-the-struct-you-want-to-embed/
CHICAGO
" » Is there any difference between struct embedding and defining a field with the struct you want to embed?." Calin Baenen | Sciencx - Accessed . https://www.scien.cx/2021/05/24/is-there-any-difference-between-struct-embedding-and-defining-a-field-with-the-struct-you-want-to-embed/
IEEE
" » Is there any difference between struct embedding and defining a field with the struct you want to embed?." Calin Baenen | Sciencx [Online]. Available: https://www.scien.cx/2021/05/24/is-there-any-difference-between-struct-embedding-and-defining-a-field-with-the-struct-you-want-to-embed/. [Accessed: ]
rf:citation
» Is there any difference between struct embedding and defining a field with the struct you want to embed? | Calin Baenen | Sciencx | https://www.scien.cx/2021/05/24/is-there-any-difference-between-struct-embedding-and-defining-a-field-with-the-struct-you-want-to-embed/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.