Home > Article > Backend Development > When Embedding in Go: Pointer or Value?
In Go, embeddability allows you to reuse fields and methods from one type within another type. However, you have the option of embedding by pointer or by value. The choice between the two can impact the behavior and efficiency of your code.
When you embed a type by pointer, the embedded type's fields are accessed through pointers. This approach is useful when:
When you embed a type by value, the embedded type's fields are copied into the embedding type. This is preferred when:
In the example provided:
type Bitmap struct{ data [4][4]bool } type Renderer struct{ *Bitmap on uint8 off uint8 }
Embedding by value is generally preferable in this case because:
The above is the detailed content of When Embedding in Go: Pointer or Value?. For more information, please follow other related articles on the PHP Chinese website!