I don't know if there's any standard practice to it. If the meaning of the
zero value and "not present" is different in your application, then
clearly, you can't use the zero value as you can't tell them apart, while
you can with a pointer. The sql package does it with a version of optionals
(https://golang.org/pkg/database/sql/#NullString), while others use
pointers as optionals. In my own project for validating API requests, I use
the swagger spec to validate (
https://github.com/deepmap/oapi-codegen#generated-server-boilerplate).

On Fri, Oct 30, 2020 at 1:45 PM Trig <edb1...@gmail.com> wrote:

> What is the standard practice between required fields of a struct you're
> expecting to deserialize using JSON, and using pointers for this (if
> there's any correlation)?
>
> API defined a string as required.  Do you define it as a string and check
> for zero-value for that type, or *string and check for nil, etc.?  Any
> other situations/instances were it's best practice to user a pointer for
> defining struct fields over the standard type?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/26d5e7f0-83e0-40eb-8274-5d9791f29b77n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/26d5e7f0-83e0-40eb-8274-5d9791f29b77n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LsDnWPMCAnDXAAWWfL0atFELF2y4KheY11QDE8dC-XN4A%40mail.gmail.com.

Reply via email to