How to Convert Slices of Structs with Different JSON Tags in Go?

Barbara Streisand
Release: 2024-10-26 18:06:30
Original
724 people have browsed it

How to Convert Slices of Structs with Different JSON Tags in Go?

Type Conversion between Slices of Structs in Go

In Go, type conversion between slices of structs can be challenging. Consider the following code:

<code class="go">type Societe struct {
    Name string
}

type ListSociete []Societe

func loadListSociete(name string) (ListSociete, error) {
    res := []struct {
        Name string `json:"a.name"`
    }{}
    // ...
    return ListSociete(res), nil
}</code>
Copy after login

When attempting to convert res to a ListSociete, it fails because these types are not inherently interchangeable. While both share a similar underlying structure, the presence of the JSON tag in res differentiates them.

Key Distinction: Tags in Struct Types

The Go specification states that two struct types are identical if they have the same sequence of fields, names, types, and tags. The res type contains the tag json:"a.name", which distinguishes it from Societe.

Conversion Options

There are two primary options for converting between these types:

  1. Iteration and Copying:

    Iterate over each element in res, creating a new ListSociete by copying the Name field from each element. While safe, this method is verbose and inefficient.

    <code class="go">ls := make(ListSociete, len(res))
    for i := 0; i < len(res); i++ {
        ls[i].Name = res[i].Name
    }</code>
    Copy after login
  2. Unsafe Conversion:

    Exploit the shared underlying data structure to perform an unsafe type conversion. However, this is not recommended as it can lead to unexpected errors.

    <code class="go">return *(*ListSociete)(unsafe.Pointer(&res)), nil</code>
    Copy after login

Conclusion

Type conversion between slices of structs can be achieved using either iteration and copying or unsafe conversion. The choice depends on the requirements and risks involved. It is important to understand the differences between the involved types and the potential consequences of any conversion approach.

The above is the detailed content of How to Convert Slices of Structs with Different JSON Tags 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!