-2

The following defines the type "Symbol" and stores the type in "SymbolType".

import (
    "reflect"
)

type Symbol string

var SymbolType reflect.Type = reflect.TypeOf(Symbol(""))

Is it possible to do this without creating an instance of "Symbol"?

Answer: It is necessary to use this cryptic pattern:

var SymbolType = reflect.TypeOf((*Symbol)(nil)).Elem()
ceving
  • 21,900
  • 13
  • 104
  • 178
  • Can you explain what you are trying to do here? `SymbolType` is always going to be `main.Symbol` regardless of the `Symbol` value. – JimB Aug 07 '23 at 14:56
  • *"Is it possible to do this without creating an instance of "Symbol"?"* -- No, the `reflect.TypeOf` function's argument MUST be a valid value, i.e. an _instance_ of some type (and note that a `nil` interface type in this case is not considered a valid instance). – mkopriva Aug 07 '23 at 18:27

0 Answers0