golang: json arrays that also have methods

WBOY
Release: 2024-02-09 10:15:18
forward
1224 people have browsed it

golang:也有方法的 json 数组

php Xiaobian Yuzi brings you an article about Golang, the theme is "golang: json arrays that also have methods". Golang is a simple and efficient programming language, and json array is one of the commonly used data formats. This article will explore how to operate on json arrays in Golang and introduce some useful methods. Whether you are a beginner or an experienced developer, this article can provide you with some valuable information and tips. Let’s find out together!

Question content

I have a data structure like this:

type (
  parent struct {
    items []*child
  }
  child struct {
    field string `json:"field"`
  }
)
Copy after login

I also hope parent has a way:

func (p *parent) example() { }
Copy after login

But the json requirement is that the parent is just an array:

[
  {
    "field": "data"
  }
]
Copy after login

I want parent to be a simple array, but in order for parent to have methods, it cannot be of array type.

Is there a way to solve these two problems with one data structure?

(To make things more complicated, the actual data structure I have to use has two levels: greatgrandparent contains []grandparent, while grandparent has A parent containing parent. The json structure is defined externally, the array has no key names, and I want methods for each of the four structures.)

Solution

In order for the parent to have methods, it cannot be an array type.

It can, it just has to have a name because only named types (or pointers to named types) can implement methods. The following is valid go code:

type parent []*child

func (p parent) example() { /* ... */ }
Copy after login

Note that the above parent is a slice rather than an array. Arrays in go have static length, you can't increase them and you can't shrink them, slices on the other hand have dynamic length, you can resize them at will. Arrays and slices are closely related, but not the same.

Another way is to have the structure type implement the json.unmarshaler/json.marshaler interface:

type parent struct { Items []*child }

func (p *parent) UnmarshalJSON(data []byte) error {
    return json.Unmarshal(data, &p.Items)
}

func (p parent) MarshalJSON() ([]byte, error) {
    return json.Marshal(p.Items)
}
Copy after login

The above will generate the required json structure.

The above is the detailed content of golang: json arrays that also have methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
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
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!