Ah sorry, I apparently didn't pay close attention then. The correct way is
to type-assert to *types.Named and then use it's Obj() method, I guess. :)


BTW: It's not "casting", but "type-asserting". "casting" (go calls it
"converting") is, when you have something with concrete type T1 and convert
it into something of concrete type T2 (changing the underlying
representation, if necessary), so e.g.
x := 42
y := float64(x)
"Type-asserting" is, when you have an interface type and are saying "please
unpack this interface; I think the concrete type X is in it" and entails a
runtime-check that the interface actually has that concrete value and then
unpack it.

i.e. type conversion ("casting") is to go from concrete type T1 to concrete
type T2, type-assertions are to go from interface type IT to concrete type
CT.

On Sun, Apr 30, 2017 at 2:05 AM, <m...@tejas.io> wrote:

> What exactly would you cast to *types.TypeName, btw? Looks like
> *types.TypeName is not a types.Type (missing Underlying() method).
>
> On Sunday, April 23, 2017 at 11:11:13 PM UTC-7, Axel Wagner wrote:
>>
>> I'd say, probably type-asserting to a *types.TypeName
>> <https://golang.org/pkg/go/types/#TypeName> and then use Exported()
>> <https://golang.org/pkg/go/types/#TypeName.Exported> (the code of which
>> also leads to ast.IsExported <https://golang.org/pkg/go/ast/#IsExported>
>> ).
>>
>> Side-note: You probably shouldn't rely on parsing the output of String()
>> of any value, just as you shouldn't rely on parsing the output of
>> error.Error().
>>
>> On Mon, Apr 24, 2017 at 7:06 AM, <m...@tejas.io> wrote:
>>
>>> https://golang.org/pkg/go/types/#Type
>>>
>>> Is there a helper function to determine whether a *types.Type* is
>>> exported? It seems like I can do some string parsing like
>>>
>>> t := something.Type()
>>> parts := strings.Split(t.String(), ".") // go/ast.Node => ["go/ast",
>>> "Node"]
>>> ident := parts[len(parts)-1] // "Node"
>>> exported := strings.IsUpper(ident[0])
>>>
>>> but I imagine there's a simpler, more robust way. The end goal is to
>>> find out whether a type of a method argument is exported-- e.g.
>>> namedType := obj.Type().(*types.Named)
>>> method := namedType.Method(0)
>>> signature := method.Type().(*types.Signature)
>>> signature.Params().At(0).Type() // is this exported?
>>>
>>> And, for some context, all of this is from walking go/ast
>>>
>>> --
>>> 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.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to