go-zero
go-zero copied to clipboard
Set the default value of a slice to a null pointer
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.
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
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"`
}
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?