How does Go\'s initialization syntax for slices and maps handle anonymous structures?

Barbara Streisand
Release: 2024-11-01 02:34:40
Original
742 people have browsed it

How does Go's initialization syntax for slices and maps handle anonymous structures?

Golang's Initialization Syntax for Slices and Maps

In Go, declaring and initializing slices and maps can involve anonymous structures, an aspect that may cause confusion. Let's delve into the nuances of these initialization techniques.

To illustrate, consider the following line from Chapter 7 of GOPL:

<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 code defines a slice of Track pointers. Within the slice initialization, the elements appear to be anonymous structures without explicit type declarations. However, Go allows you to initialize these structures using a syntax shortcut:

<code class="go">f := []<type>{{...}, {...}}</code>
Copy after login

This syntax is effectively equivalent to:

<code class="go">f := []<type>{<type>{...}, <type>{...}}</code>
Copy after login

where can represent any type, including pointers. Thus, in the case of the tracks slice, the following initialization is valid:

<code class="go">f := []*Track{{...}, {...}}</code>
Copy after login

which is equivalent to:

<code class="go">f := []*Track{&Track{...}, &Track{...}}</code>
Copy after login

This syntax also applies to maps:

<code class="go">f := map[string]<type>{"a": {...}, "b": {...}} // equivalent to
f := map[string]<type>{"a": &<type>{...}, "b": &<type>{...}}</code>
Copy after login

Anonymous structures, on the other hand, refer to structures without a separate type definition. Anonymous types are distinct from anonymous structures, where only the structure itself lacks a name. A true anonymous structure is written as:

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

In this case, the struct within the list has no type definition and is referred to anonymously. The initialization shorthand allows us to conveniently initialize this structure without a named type.

The above is the detailed content of How does Go\'s initialization syntax for slices and maps handle anonymous structures?. 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!