Home  >  Article  >  Backend Development  >  How to use non-required JSON parameters in Go?

How to use non-required JSON parameters in Go?

王林
王林forward
2024-02-12 09:10:08931browse

如何在 Go 中使用非必需的 JSON 参数?

php Xiaobian Yuzai brings you tips on how to use non-required JSON parameters in the Go language. When writing Go programs, we often need to process JSON data. Sometimes we may only need to use some of the parameters, but not all of them. This article will introduce how to use non-required JSON parameters in Go, allowing you to process JSON data more flexibly and improve the readability and maintainability of your code. Whether you are a beginner or an experienced developer, this article will provide you with useful tips and sample code to help you better understand and apply this feature. Let’s explore together!

Question content

Hello, I am developing Rest API in Go and I want the user to pass JSON parameters:

Offset int64  `json:"offset"`
Limit  int64  `json:"limit"`
SortBy string `json:"sortby"`
Asc    bool   `json:"asc"`
Username   string `json:"username"`
First_Name string `json:"first_name"`
Last_Name  string `json:"last_name"`
Status     string `json:"status"`

But they are not always required, for example the user can pass only Offset and ignore the others. He can even send 0 parameters. How can I do this?

Workaround

When unmarshaling values ​​from JSON text, the json package does not require that all fields appear in the JSON, nor does it guarantee that all JSON fields have matching Go fields.

So you don't have anything special to do, just organize what you have into Go values ​​for what you want or might want.

One thing to note is that if a field is missing from the JSON text, the json package will not change the corresponding Go field, so if it starts with "fresh", zero value, the field will retain the zero value of its type.

Most of the time this is enough to detect if a field (in JSON) exists, for example if in a Go struct you have a SortBy field of type string, if JSON The field is missing from it, it will remain empty string: "".

If the zero value is useful and valid, then you can move to using pointers. For example, if in your application a null string would be a valid SortBy value, you could declare this field as a pointer: *string. In this case, if it is missing from the JSON text, it will retain nil, i.e. the zero value of any pointer type.

See this example:

type Data struct {
    I int
    S string
    P *string
}

func main() {
    var d Data
    var err error

    d, err = Data{}, nil
    err = json.Unmarshal([]byte(`{"I":1, "S":"sv", "P":"pv"}`), &d)
    fmt.Printf("%#v %v\n", d, err)

    d, err = Data{}, nil
    err = json.Unmarshal([]byte(`{"I":1}`), &d)
    fmt.Printf("%#v %v\n", d, err)

    d, err = Data{}, nil
    err = json.Unmarshal([]byte(`{"S":"abc"}`), &d)
    fmt.Printf("%#v %v\n", d, err)
}

Output (try it on Go Playground):

main.Data{I:1, S:"sv", P:(*string)(0x1050a150)} <nil>
main.Data{I:1, S:"", P:(*string)(nil)} <nil>
main.Data{I:0, S:"abc", P:(*string)(nil)} <nil>

The above is the detailed content of How to use non-required JSON parameters in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete