On Wed, Nov 30, 2022 at 5:29 AM 'Mark' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> I have this code which works but has a horrible hack:
> ...
> nullable := false
> kind := field.Kind() // field's type is reflect.Value
> if kind == reflect.Ptr {
>

This should work:

kind = field.Elem().Kind()




>     // FIXME How can I improve upon this truly awful hack?
>     switch field.Type().String() {
>     case "*int", "*int8", "*uint8", "*int16", "*uint16", "*int32",
> "*uint32", "*int64", "*uint64":
>         kind = reflect.Int
>     case "*float32", "*float64":
>         kind = reflect.Float64
>     }
>     nullable = true
> }
> switch kind {
> case reflect.Bool:
>     out.WriteString("bool")
> case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
> reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
> reflect.Uint64:
>     out.WriteString("int")
> case reflect.Float32, reflect.Float64:
>     out.WriteString("real")
> ...
> if nullable {
>     out.WriteByte('?')
> }
> What is the correct way to achieve what I'm aiming for?
>
> --
> 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/5ff4b6f6-405c-4ca5-9299-7c15e1d5c424n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/5ff4b6f6-405c-4ca5-9299-7c15e1d5c424n%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/CAMV2RqpQXU%2B3b-v5UwB45initW7Yk99CT8k%3DdK%3DkHW%3DGJC6-aA%40mail.gmail.com.

Reply via email to