On Wed, Nov 30, 2022 at 10:17 AM 'Mark' via golang-nuts < golang-nuts@googlegroups.com> wrote:
> Yes, I'd already tried that (that's what I started with) and unfortunately > it doesn't work. > It fails if field.Elem() is nil. Try this: kind = field.Type().Elem().Kind() > > On Wednesday, November 30, 2022 at 3:37:47 PM UTC bse...@computer.org > wrote: > >> On Wed, Nov 30, 2022 at 5:29 AM 'Mark' via golang-nuts < >> golan...@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...@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/c2d154f2-b425-4cc9-a015-af30f4dc9de2n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/c2d154f2-b425-4cc9-a015-af30f4dc9de2n%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/CAMV2RqpNMsPVzpEPM-WFJpcQiOmvruHhQ7h9Ls7B_BpSj219rw%40mail.gmail.com.