本文整理汇总了Golang中reflect.MakeChan函数的典型用法代码示例。如果您正苦于以下问题:Golang MakeChan函数的具体用法?Golang MakeChan怎么用?Golang MakeChan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MakeChan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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>
}
示例3: MakeChannel
func MakeChannel(id string,
fromClientStruct interface{},
toClientStruct interface {})(fromClient unknownTypeChan,toclient unknownTypeChan) {
fromClient=reflect.MakeChan(fromClientStruct,1)
toclient=reflect.MakeChan(toClientStruct,1)
}
示例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: 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()
}
示例6: TakeChan
// TakeChan is of type: func(num int, input chan T) chan T.
// Accept only the given number of items from the input chan. After that number
// has been received, all input messages will be ignored and the output channel
// will be closed.
func TakeChan(num int, input interface{}) interface{} {
inputValue := reflect.ValueOf(input)
if inputValue.Kind() != reflect.Chan {
panic(fmt.Sprintf("DropChan called on invalid type: %s", inputValue.Type()))
}
output := reflect.MakeChan(inputValue.Type(), 0)
var count int
go func() {
// only send num items
for count = 0; count < num; count++ {
item, ok := inputValue.Recv()
if !ok {
break
}
output.Send(item)
}
// sent our max, close the channel
output.Close()
}()
return output.Interface()
}
示例7: FilterChan
// FilterChan is of type: func(fn func(T) bool, input chan T) chan T.
// Apply a filtering function to a chan, which will only pass through
// items when the filter func returns true.
func FilterChan(fn, input interface{}) interface{} {
checkFilterFuncType(fn, input)
inputValue := reflect.ValueOf(input)
fnValue := reflect.ValueOf(fn)
if inputValue.Kind() != reflect.Chan {
panic(fmt.Sprintf("FilterChan called on invalid type: %s", inputValue.Type()))
}
output := reflect.MakeChan(inputValue.Type(), 0)
go func() {
for {
item, ok := inputValue.Recv()
if !ok {
break
}
if fnValue.Call([]reflect.Value{item})[0].Bool() {
output.Send(item)
}
}
output.Close()
}()
return output.Interface()
}
示例8: zero
func zero(head P, mid string, typ reflect.Type) (value reflect.Value, err error) {
switch typ.Kind() {
case reflect.Ptr:
value = reflect.New(typ.Elem())
case reflect.Map:
value = reflect.MakeMap(typ)
case reflect.Slice:
value = reflect.MakeSlice(typ, 0, 0)
case reflect.Chan:
value = reflect.MakeChan(typ, 1)
case reflect.Func, reflect.Interface, reflect.UnsafePointer:
value = reflect.Zero(typ)
case reflect.Invalid:
err = fmt.Errorf("unable to create '%s' at '%s'", typ, append(head, mid))
default:
value = reflect.Zero(typ)
}
return
}
示例9: DropChan
// DropChan is of type: func(num int, input chan T) chan T.
// Drop a given number of items from the input chan. After that number has been
// dropped, the rest are passed straight through.
func DropChan(num int, input interface{}) interface{} {
inputValue := reflect.ValueOf(input)
if inputValue.Kind() != reflect.Chan {
panic(fmt.Sprintf("DropChan called on invalid type: %s", inputValue.Type()))
}
output := reflect.MakeChan(inputValue.Type(), 0)
var count int
go func() {
// drop num items
for count = 0; count < num; count++ {
_, ok := inputValue.Recv()
if !ok {
// channel closed early
output.Close()
return
}
}
// Return the rest
for {
item, ok := inputValue.Recv()
if !ok {
break
}
output.Send(item)
}
output.Close()
}()
return output.Interface()
}
示例10: TakeWhileChan
// TakeWhileChan is of type: func(fn func(T) bool, input chan T) chan T.
// Accept items from the input chan until the given function returns false.
// After that, all input messages will be ignored and the output channel will
// be closed.
func TakeWhileChan(fn, input interface{}) interface{} {
checkTakeWhileFuncType(fn, input)
inputValue := reflect.ValueOf(input)
fnValue := reflect.ValueOf(fn)
output := reflect.MakeChan(inputValue.Type(), 0)
go func() {
for {
item, ok := inputValue.Recv()
if !ok {
break
}
// check if we should continue
if !fnValue.Call([]reflect.Value{item})[0].Bool() {
break
}
output.Send(item)
}
// hit the toggle, close the channel
output.Close()
// drop any extra messages
for {
_, ok := inputValue.Recv()
if !ok {
break
}
}
}()
return output.Interface()
}
示例11: evalBuiltinMakeExpr
func evalBuiltinMakeExpr(ctx *Ctx, call *CallExpr, env *Env) ([]reflect.Value, error) {
resT := call.KnownType()[0]
length, capacity := 0, 0
var err error
if len(call.Args) > 1 {
if length, err = evalInteger(ctx, call.Args[1].(Expr), env); err != nil {
return nil, err
}
}
if len(call.Args) > 2 {
if capacity, err = evalInteger(ctx, call.Args[2].(Expr), env); err != nil {
return nil, err
}
}
var res reflect.Value
switch resT.Kind() {
case reflect.Slice:
res = reflect.MakeSlice(resT, length, capacity)
case reflect.Map:
res = reflect.MakeMap(resT)
case reflect.Chan:
res = reflect.MakeChan(resT, length)
default:
panic(dytc("make(bad type)"))
}
return []reflect.Value{res}, nil
}
示例12: 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()
}
示例13: 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
}
示例14: Reset
// Reset sets all elements of the object pointed to
// by resultRef to their default or zero values.
// But it works different from simply zeroing out everything,
// here are the exceptions:
// If resultRef is a pointer to a pointer, then
// the pointed to pointer will be reset to a new instance
// If resultRef is a pointer to a map, then the map
// will be reset to a new empty one.
// All other types pointed to by resultRef will be set
// to their default zero values.
func Reset(resultRef interface{}) {
ptr := reflect.ValueOf(resultRef)
if ptr.Kind() != reflect.Ptr {
panic(fmt.Errorf("reflection.Reset(): resultRef must be a pointer, got %T", resultRef))
}
val := ptr.Elem()
switch val.Kind() {
case reflect.Ptr:
// If resultRef is a pointer to a pointer,
// set the pointer to a new instance
// of the pointed to type
val.Set(reflect.New(val.Type().Elem()))
case reflect.Map:
// If resultRef is a pointer to a map,
// set make an empty new map
val.Set(reflect.MakeChan(val.Type(), 0))
case reflect.Struct:
SetStructZero(val)
default:
val.Set(reflect.Zero(val.Type()))
}
}
示例15: makeChan
func makeChan(typ reflect.Type, buffer ...int) interface{} {
n := 0
if len(buffer) > 0 {
n = buffer[0]
}
return &qlang.Chan{Data: reflect.MakeChan(typ, n)}
}