Home >Backend Development >Golang >How to Convert Slices of Structs with Different JSON Tags in Go?

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

Barbara Streisand
Barbara StreisandOriginal
2024-10-26 18:06:30801browse

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>

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>
  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>

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!

Statement:
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