On Wed, Nov 2, 2016 at 7:08 AM,  <acon...@redhat.com> wrote:
>
> This seems like it should be simple but I can't find it. How can I create a
> reflect.Type for a built-in type *without* starting from an instance?
> Something like a set of Type constants,  reflect.IntType, reflect.StringType
> etc. for all bulit-in types.

This does not exist, because it is so easy to simply write
reflect.TypeOf(int(0)).


> I'd like to do this:
>
> func myChanT(t reflect.Type) interface{} { ch = reflect.MakeChan(t, n);
> ...something...; return ch.Interface() }
>
>
> and then be able to call myChanT to create a channel of int:
> ch := myChan(reflect.IntType)
>
> I could make my own constants:
>
> const (
>    UIntType Type = TypeOf(uint(0))
>   ....
> }
>
> but it seems strange that it isn't already in the reflect package.
>
> I could also have myChan operate on an existing channel and reflect on that:
>
> func myChanT(ch interface{}) { ...reflect on ch... }
>
> but I would prefer to control the creation and direction-safety of my
> channels, i.e. I want myChanT to make a chan T internally, but return a
> ->chan T or a chan-> T to ensure the caller can't mess with the wrong end.

You could also write

func myChan(v interface{}) interface{} {
    ch = reflect.MakeChan(reflect.TypeOf(v), n)
    ...
}

Then the caller would pass in a value of the type they want the
channel to be of (e.g., int(0)) rather than a reflect.Type.

Ian

-- 
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