0

I have a type like this

type Transformer func(raw string) (*interface{}, error)

I need a function for boolean

var TransformBoolean Transformer = func(raw string) (*interface{}, error) {
     var value bool
     if raw == "true" {
          value = true
     } else if raw == "false" {
          value = false
     } else {
          return nil, Error{"Invalid"}
     }
     return &value, nil // <-- Here's the error
}

The function return type is interface{} because it can return ints, booleans, floats, etc. But, the compiler doesn't let me return a *boolean as a *interface{}

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

value is a boolean so &value is a pointer to an boolean (not a pointer to an interface{}). If you want to return a pointer to an interface then you could use:

x := interface{}(value)
return &x, nil

See Playground.

However there are not many situations where taking the address of an interface is useful (see the answer to this question) so it may be worth considering whether this is necessary.

Brits
  • 14,829
  • 2
  • 18
  • 31