Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the default value of a slice to a null pointer #3784

Open
1067088037 opened this issue Dec 12, 2023 · 4 comments
Open

Set the default value of a slice to a null pointer #3784

1067088037 opened this issue Dec 12, 2023 · 4 comments
Labels

Comments

@1067088037
Copy link
Contributor

User {
    Links []string `json:"links,default="`
}

This is a API file. How to set the default value of this slice to a null pointer?
I tried setting it to nil but it didn't work.

@POABOB
Copy link
Contributor

POABOB commented Dec 18, 2023

If you want to set the field value to null when there are no links in the JSON payload.

Please try to use a pointer as shown below:

User {
    Links *[]string `json:"links,optional"`
}

reference: https://go-zero.dev/en/docs/tutorials/api/parameter

@1067088037
Copy link
Contributor Author

I tried it but got an error.
Error: user.api line 84:22 mismatched input '[' expecting ID

It's valid to write it this way.

User {
    Links []string `json:"links,optional"`
}

@POABOB
Copy link
Contributor

POABOB commented Dec 18, 2023

Sorry, it seems that api file doesn't support pointer type of slice data.
Why you want a null pointer value not a empty slice?

Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants