When to Use a Pointer vs. Value Reference for Embedded Fields in Go?

DDD
Release: 2024-10-30 23:47:29
Original
763 people have browsed it

When to Use a Pointer vs. Value Reference for Embedded Fields in Go?

Embedding: When to Employ a Pointer vs. Value Reference

When incorporating one structure within another in Golang, the choice arises between using a pointer or a value reference. Consider the following code snippet:

<code class="go">type Job struct {
    Command string
    *log.Logger
}</code>
Copy after login

In this example, we have embedded the *log.Logger type, represented by a pointer, within the Job structure. Alternatively, we could have utilized a value reference as below:

<code class="go">type Job struct {
    Command string
    log.Logger
}</code>
Copy after login

While both approaches achieve embedding, their implications differ. According to the Go specification, embedded fields support either the type itself or a pointer to a non-interface type. Crucially, the embedded type cannot be a pointer type.

Embedding a pointer, known as "embed by-pointer," offers specific advantages. Firstly, it allows leveraging functions that follow the NewX pattern, where structures are initialized and returned by reference. Secondly, it enables dynamic assignment of different instances to the embedded type at runtime.

For instance, consider the following code:

<code class="go">type Bitmap struct{
    data [4][5]bool
}

type Renderer struct{
    *Bitmap //Embed by pointer
    on uint8
    off uint8
}</code>
Copy after login

In this example, the Renderer type embeds a Bitmap by reference. This allows a single instance of Bitmap to serve as the embedded instance for several Renderer instances, each with its unique set of characters. The output of this code demonstrates how multiple renderers can operate on the same underlying data structure.

OXXO
OXOO
OXOO
OXOO
.@@.
.@..
.@..
.@..
Copy after login

In contrast, embedding a value reference does not provide these advantages. However, it does not require the instantiation of an embedded type to access its methods. Ultimately, the decision between using a pointer or a value reference depends on the specific requirements of the application and the behaviors desired for embedded fields.

The above is the detailed content of When to Use a Pointer vs. Value Reference for Embedded Fields in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!