Home > Backend Development > Golang > How Can I Efficiently Initialize Embedded Structs in Go?

How Can I Efficiently Initialize Embedded Structs in Go?

Mary-Kate Olsen
Release: 2024-12-17 01:03:24
Original
701 people have browsed it

How Can I Efficiently Initialize Embedded Structs in Go?

Struct Initialization with Embedded Structs in Go

Composing structs is a common practice in Go for code organization and reuse. However, initializing composed structs can present a challenge when a subfield is inherited from an embedded struct.

Consider the following example:

type Base struct {
    ID string
}

type Child struct {
    Base
    a int
    b int
}
Copy after login

To initialize Child, one might expect to use the following syntax:

child := Child{ ID: id, a: a, b: b }
Copy after login
Copy after login

However, this will result in a compilation error due to the unknown field ID in the Child struct literal. To workaround this issue, the ID field must be initialized separately:

child := Child{ a: 23, b: 42 }
child.ID = "foo"
Copy after login

This approach violates encapsulation principles by exposing the fact that ID is an embedded field. It also introduces the risk of breaking initialization if a public field is moved into an embedded struct.

To address this issue, there are two recommended solutions:

  1. Nested Composite Literals:

    Nested composite literals can be used to initialize an embedded field in a single expression:

    child := Child{Base: Base{ID: id}, a: a, b: b}
    Copy after login
  2. Proposed Change in Go:

    Go issue 9859 proposes a change to make composite literals consistent with field access for embedded types. If implemented, this change would allow the following syntax:

    child := Child{ ID: id, a: a, b: b }
    Copy after login
    Copy after login

It's important to note that embedded types do not provide encapsulation in the true sense. An application can still directly access child.Base.ID despite using the child.ID syntax.

The above is the detailed content of How Can I Efficiently Initialize Embedded Structs 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template