#How to get private struct type for a type switch?(without reflection)

2 messages · Page 1 of 1 (latest)

fading prawn
#

Hello, I am looking for a way to get the typeinfo of a private struct so I can cast an interface to the wanted type.
I am aware that I can for example use reflection or an interface to get like a string representation of the type and then make a string switch, but I am looking for a build in way or just a better/best way in general.

// package A
type Example interface{
    DoSmth()
}

// package B
type person struct {
    age int
    name string
}

func NewPerson(name string, age int) *person {
    return &person{age, name}
}

// implement Example interface
func (obj *person) DoSmth() {
}

// package C

func Casting(inter A.Example) {
     switch inter.(type) {
     case *B.person: // <---- Does not work since its private
         temp := inter.(*B.person) // <---- Does not work
     }
}

Thanks in advance!

nocturne scroll
#

@fading prawn you can't access the symbol of a private struct, you can try making it publpic