本文整理汇总了Golang中reflect.ChanOf函数的典型用法代码示例。如果您正苦于以下问题:Golang ChanOf函数的具体用法?Golang ChanOf怎么用?Golang ChanOf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ChanOf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
var i int
var recvDir reflect.ChanDir = reflect.RecvDir
var chanOf reflect.Type = reflect.ChanOf(recvDir, reflect.TypeOf(i))
fmt.Println(chanOf.Kind()) // chan
fmt.Println(chanOf.ChanDir()) // <-chan
fmt.Println(chanOf.String()) // <-chan int
var i1 int
var recvDir1 reflect.ChanDir = reflect.SendDir
var chanOf1 reflect.Type = reflect.ChanOf(recvDir1, reflect.TypeOf(i1))
fmt.Println(chanOf1.Kind(), chanOf1.ChanDir(), chanOf1.String())
// chan chan<- chan<- int
var i2 int
var recvDir2 reflect.ChanDir = reflect.BothDir
var chanOf2 reflect.Type = reflect.ChanOf(recvDir2, reflect.TypeOf(i2))
fmt.Println(chanOf2.Kind(), chanOf2.ChanDir(), chanOf2.String())
// chan chan chan int
var i3 string
var recvDir3 reflect.ChanDir = reflect.BothDir
var chanOf3 reflect.Type = reflect.ChanOf(recvDir3, reflect.TypeOf(i3))
fmt.Println(chanOf3.Kind(), chanOf3.ChanDir(), chanOf3.String())
// chan chan chan string
}
示例2: Test_InjectorInvoke
func Test_InjectorInvoke(t *testing.T) {
injector := inject.New()
expect(t, injector == nil, false)
dep := "some dependency"
injector.Map(dep)
dep2 := "another dep"
injector.MapTo(dep2, (*SpecialString)(nil))
dep3 := make(chan *SpecialString)
dep4 := make(chan *SpecialString)
typRecv := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(dep3).Elem())
typSend := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(dep4).Elem())
injector.Set(typRecv, reflect.ValueOf(dep3))
injector.Set(typSend, reflect.ValueOf(dep4))
_, err := injector.Invoke(func(d1 string, d2 SpecialString, d3 <-chan *SpecialString, d4 chan<- *SpecialString) {
expect(t, d1, dep)
expect(t, d2, dep2)
expect(t, reflect.TypeOf(d3).Elem(), reflect.TypeOf(dep3).Elem())
expect(t, reflect.TypeOf(d4).Elem(), reflect.TypeOf(dep4).Elem())
expect(t, reflect.TypeOf(d3).ChanDir(), reflect.RecvDir)
expect(t, reflect.TypeOf(d4).ChanDir(), reflect.SendDir)
})
expect(t, err, nil)
}
示例3: mapDefaultChannels
// Helper method to map default channels in the context
func (c *Connection) mapDefaultChannels(context martini.Context) {
// Map the Error Channel to a <-chan error for the next Handler(s)
context.Set(reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(c.Error).Elem()), reflect.ValueOf(c.Error))
// Map the Disconnect Channel to a chan<- bool for the next Handler(s)
context.Set(reflect.ChanOf(reflect.SendDir, reflect.TypeOf(c.Disconnect).Elem()), reflect.ValueOf(c.Disconnect))
// Map the Done Channel to a <-chan bool for the next Handler(s)
context.Set(reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(c.Done).Elem()), reflect.ValueOf(c.Done))
}
示例4: NewChan
func NewChan(typ interface{}) (interface{}, interface{}) {
rc := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, reflect.TypeOf(typ)), 0)
wc := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, reflect.TypeOf(typ)), 0)
go loop(rc, wc)
vrc := rc.Convert(reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(typ)))
vwc := wc.Convert(reflect.ChanOf(reflect.SendDir, reflect.TypeOf(typ)))
return vrc.Interface(), vwc.Interface()
}
示例5: Test_Injector_Invoke
func Test_Injector_Invoke(t *testing.T) {
Convey("Invokes function", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
dep := "some dependency"
injector.Map(dep)
dep2 := "another dep"
injector.MapTo(dep2, (*SpecialString)(nil))
dep3 := make(chan *SpecialString)
dep4 := make(chan *SpecialString)
typRecv := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(dep3).Elem())
typSend := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(dep4).Elem())
injector.Set(typRecv, reflect.ValueOf(dep3))
injector.Set(typSend, reflect.ValueOf(dep4))
_, err := injector.Invoke(func(d1 string, d2 SpecialString, d3 <-chan *SpecialString, d4 chan<- *SpecialString) {
So(d1, ShouldEqual, dep)
So(d2, ShouldEqual, dep2)
So(reflect.TypeOf(d3).Elem(), ShouldEqual, reflect.TypeOf(dep3).Elem())
So(reflect.TypeOf(d4).Elem(), ShouldEqual, reflect.TypeOf(dep4).Elem())
So(reflect.TypeOf(d3).ChanDir(), ShouldEqual, reflect.RecvDir)
So(reflect.TypeOf(d4).ChanDir(), ShouldEqual, reflect.SendDir)
})
So(err, ShouldBeNil)
_, err = injector.Invoke(myFastInvoker(func(string) {}))
So(err, ShouldBeNil)
})
Convey("Invokes function with return value", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
dep := "some dependency"
injector.Map(dep)
dep2 := "another dep"
injector.MapTo(dep2, (*SpecialString)(nil))
result, err := injector.Invoke(func(d1 string, d2 SpecialString) string {
So(d1, ShouldEqual, dep)
So(d2, ShouldEqual, dep2)
return "Hello world"
})
So(result[0].String(), ShouldEqual, "Hello world")
So(err, ShouldBeNil)
})
}
示例6: TestGetChannel
func TestGetChannel(t *testing.T) {
b := &Broadcaster{RoutineMaxClients: 10}
var uids []string
var tids []int
for i := 0; i < 10; i++ {
tuid, ttid := b.AddClient()
uids = append(uids, tuid)
tids = append(tids, ttid)
}
ch, err := b.GetChannel(uids[1], tids[1])
if err != nil {
t.Errorf("Test encountered the following error: %v", err)
}
if reflect.TypeOf(ch) != reflect.ChanOf(reflect.BothDir, reflect.TypeOf("")) {
t.Errorf("Test expected to retrieve channel, but received %v", reflect.TypeOf(ch))
}
ch, err = b.GetChannel("asd", 3)
if err == nil {
t.Error("An error was expected, but not received")
}
}
示例7: chanOfUnknown
func chanOfUnknown() {
// Unknown channel direction: assume all three.
// MakeChan only works on the bi-di channel type.
t := reflect.ChanOf(unknownDir, reflect.TypeOf(&a))
print(reflect.Zero(t).Interface()) // @types <-chan *int | chan<- *int | chan *int
print(reflect.MakeChan(t, 0).Interface()) // @types chan *int
}
示例8: chanOfSend
func chanOfSend() {
// MakeChan(chan<-) is a no-op.
t := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(&a))
print(reflect.Zero(t).Interface()) // @types <-chan *int | chan<- *int | chan *int
print(reflect.MakeChan(t, 0).Interface().(chan<- *int)) // @pointsto
print(reflect.MakeChan(t, 0).Interface().(chan *int)) // @pointsto <alloc in reflect.MakeChan>
}
示例9: chanOfRecv
func chanOfRecv() {
// MakeChan(<-chan) is a no-op.
t := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(&a))
print(reflect.Zero(t).Interface()) // @types <-chan *int
print(reflect.MakeChan(t, 0).Interface().(<-chan *int)) // @pointsto
print(reflect.MakeChan(t, 0).Interface().(chan *int)) // @pointsto
}
示例10: executeCmd
// executeCmd runs Store.ExecuteCmd in a goroutine. A channel with
// element type equal to the reply type is created and returned
// immediately. The reply is sent to the channel once the cmd has been
// executed by the store. The store is looked up from the store map
// if specified by header.Replica; otherwise, the command is being
// executed locally, and the replica is determined via lookup of
// header.Key in the ranges slice.
func (db *LocalDB) executeCmd(method string, header *storage.RequestHeader, args, reply interface{}) interface{} {
chanVal := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, reflect.TypeOf(reply)), 1)
replyVal := reflect.ValueOf(reply)
go func() {
// If the replica isn't specified in the header, look it up.
var err error
var store *storage.Store
// If we aren't given a Replica, then a little bending over backwards here. We need to find the Store, but all
// we have is the Key. So find its Range locally, and pull out its Replica which we use to find the Store.
// This lets us use the same codepath below (store.ExecuteCmd) for both locally and remotely originated
// commands.
if header.Replica.NodeID == 0 {
if repl := db.lookupReplica(header.Key); repl != nil {
header.Replica = *repl
} else {
err = util.Errorf("unable to lookup range replica for key %q", string(header.Key))
}
}
if err == nil {
store, err = db.GetStore(&header.Replica)
}
if err != nil {
reflect.Indirect(replyVal).FieldByName("Error").Set(reflect.ValueOf(err))
} else {
store.ExecuteCmd(method, header, args, reply)
}
chanVal.Send(replyVal)
}()
return chanVal.Interface()
}
示例11: tysubst
// tysubst attempts to substitute all type variables within a single return
// type with their corresponding Go type from the type environment.
//
// tysubst will panic if a type variable is unbound, or if it encounters a
// type that cannot be dynamically created. Such types include arrays,
// functions and structs. (A limitation of the `reflect` package.)
func (rt returnType) tysubst(typ reflect.Type) reflect.Type {
if tyname := tyvarName(typ); len(tyname) > 0 {
if thetype, ok := rt.tyenv[tyname]; !ok {
rt.panic("Unbound type variable %s.", tyname)
} else {
return thetype
}
}
switch typ.Kind() {
case reflect.Array:
rt.panic("Cannot dynamically create Array types.")
case reflect.Chan:
return reflect.ChanOf(typ.ChanDir(), rt.tysubst(typ.Elem()))
case reflect.Func:
rt.panic("Cannot dynamically create Function types.")
case reflect.Interface:
rt.panic("TODO")
case reflect.Map:
return reflect.MapOf(rt.tysubst(typ.Key()), rt.tysubst(typ.Elem()))
case reflect.Ptr:
return reflect.PtrTo(rt.tysubst(typ.Elem()))
case reflect.Slice:
return reflect.SliceOf(rt.tysubst(typ.Elem()))
case reflect.Struct:
rt.panic("Cannot dynamically create Struct types.")
case reflect.UnsafePointer:
rt.panic("Cannot dynamically create unsafe.Pointer types.")
}
// We've covered all the composite types, so we're only left with
// base types.
return typ
}
示例12: main
func main() {
t := reflect.TypeOf(3)
chtype := reflect.ChanOf(reflect.RecvDir, t)
inst := *(reflect.New(chtype).Interface().(*<-chan int))
fmt.Println(inst)
}
示例13: FlattenChan
// FlattenChan is of type: func(input chan []T) chan T.
// Takes a chan of arrays, and concatenates them together, putting each element
// onto the output chan. After input is closed, output is also closed. If input
// is chan T instead of type chan []T, then this is a no-op.
func FlattenChan(input interface{}) interface{} {
inputValue := reflect.ValueOf(input)
if inputValue.Kind() != reflect.Chan {
panic(fmt.Sprintf("FlattenChan called on invalid type: %s", inputValue.Type()))
}
elemType := inputValue.Type().Elem()
if elemType.Kind() != reflect.Array &&
elemType.Kind() != reflect.Slice {
return input
}
outputType := reflect.ChanOf(reflect.BothDir, elemType.Elem())
output := reflect.MakeChan(outputType, 0)
go func() {
for {
value, ok := inputValue.Recv()
if !ok {
break
}
for i := 0; i < value.Len(); i++ {
output.Send(value.Index(i))
}
}
output.Close()
}()
return output.Interface()
}
示例14: makeWorkerChans
// makeWorkerChans makes a buffered channel of the specified type
func makeWorkerChans(t reflect.Type) (chan []reflect.Value, reflect.Value) {
// display(reflect.TypeOf([]reflect.Value{}))
// job := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, reflect.TypeOf(&channeller{})), 100)
// job := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, reflect.TypeOf([]reflect.Value{})), 100)
job := make(chan []reflect.Value)
res := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, t), 100)
return job, res
}
示例15: main
func main() {
c := reflect.ChanOf(reflect.SendDir|reflect.RecvDir, Int)
fmt.Println(c)
t := reflect.TypeOf(make(chan int)).Elem()
fmt.Println(t)
}