How to Initialize Slices of Pointers with Anonymous Structs in Golang?

Mary-Kate Olsen
Release: 2024-11-01 15:53:02
Original
948 people have browsed it

How to Initialize Slices of Pointers with Anonymous Structs in Golang?

Initializing Slices of Pointers with Anonymous Structs in Golang

In the code snippet from Chapter 7 of GOPL (Section 7.6), an anonymous struct is used to initialize a slice of pointers:

<code class="go">var tracks = []*Track{
    {"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
    {"Go", "Moby", "Moby", 1992, length("3m37s")},
    {"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
    {"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
}</code>
Copy after login

This may seem confusing as the Track type is already defined, and the syntax {1, 2} appears to be initializing a new, unnamed struct. However, this syntax is a shortcut used to initialize the element of the slice type without explicitly defining the struct.

The "{}" notation is essentially equivalent to:

<code class="go">type Ex struct {
    A, B int
}

a := []Ex{Ex{1, 2}, Ex{3, 4}}</code>
Copy after login

Where the type name (Ex) is omitted. This syntax works even when initializing slices of pointers:

<code class="go">c := []*Ex{&Ex{1, 2}, &Ex{3, 4}}
d := []*Ex{{1, 2}, {3, 4}}
e := []*Ex{{1, 2}, &Ex{3, 4}}</code>
Copy after login

In these cases, the & operator is automatically applied to the anonymous struct to create the pointer.

However, attempting to initialize a slice of pointers with a reference to an anonymous struct (without the & operator) will result in a syntax error:

<code class="go">f := []*Ex{&{1, 2}, &{3, 4}} // Syntax Error!</code>
Copy after login

To initialize a slice of pointers with an anonymous struct, either the anonymous struct must be converted to a pointer, or the pointer can be omitted altogether:

<code class="go">f := []struct{
    A, B int
}{
    {1, 2}, {3, 4}
}
// or with pointers...
g := []*struct{
    A, B int
}{
    {1, 2}, {3, 4}
}</code>
Copy after login

Here, the anonymous struct is embedded within the slice of pointers without the need for a separate type definition. This syntax is commonly used to initialize anonymous structs in maps and other collections in Golang.

The above is the detailed content of How to Initialize Slices of Pointers with Anonymous Structs in Golang?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!