本文整理汇总了Golang中reflect.Value.Recv方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Recv方法的具体用法?Golang Value.Recv怎么用?Golang Value.Recv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Recv方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handle
func (cnn *serverConn) handle(chpv reflect.Value, name []byte) {
handler := func(ws *websocket.Conn) {
// send
go func() {
for {
if v, ok := chpv.Recv(); ok {
cnn.codec.Send(ws, v)
} else {
panic(Closed)
}
}
}()
// recieve
for {
v := reflect.New(chpv.Type().Elem())
cnn.codec.Receive(ws, v.Interface())
chpv.Send(reflect.Indirect(v))
}
}
encodedName := base64.URLEncoding.EncodeToString(name)
if _, ok := cnn.channels[encodedName]; !ok {
cnn.channels[encodedName] = true
pattern := fmt.Sprintf("/%s", encodedName)
cnn.handlers.Handle(pattern, websocket.Handler(handler))
}
}
示例2: chPublish
// Publish all values that arrive on the channel until it is closed or we
// encounter an error.
func chPublish(c *EncodedConn, chVal reflect.Value, subject string) {
for {
val, ok := chVal.Recv()
if !ok {
// Channel has most likely been closed.
return
}
if e := c.Publish(subject, val.Interface()); e != nil {
// Do this under lock.
c.Conn.mu.Lock()
defer c.Conn.mu.Unlock()
if c.Conn.Opts.AsyncErrorCB != nil {
// FIXME(dlc) - Not sure this is the right thing to do.
// FIXME(ivan) - If the connection is not yet closed, try to schedule the callback
if c.Conn.isClosed() {
go c.Conn.Opts.AsyncErrorCB(c.Conn, nil, e)
} else {
c.Conn.ach <- func() { c.Conn.Opts.AsyncErrorCB(c.Conn, nil, e) }
}
}
return
}
}
}
示例3: applyToChan
func applyToChan(obj reflect.Value, head P, mid string, tail P, ctx *Context) error {
if dir := obj.Type().ChanDir(); dir != reflect.RecvDir && dir != reflect.BothDir {
return fmt.Errorf("invalid channel direction '%s' at '%s'", dir, head)
}
switch mid {
case "1":
result, ok := obj.Recv()
if !ok {
return ErrMissing
}
return apply(result, append(head, mid), tail, ctx)
case "*":
for !ctx.stop {
result, ok := obj.Recv()
if !ok {
return nil
}
if err := apply(result, append(head, mid), tail, ctx); err != nil && err != ErrMissing {
return err
}
}
return nil
default:
return fmt.Errorf("invalid channel component '%s' at '%s'", mid, head)
}
}
示例4: iterateChan
func iterateChan(valueOfIter reflect.Value, out chan Pair) {
i := 0
for v, ok := valueOfIter.Recv(); ok; v, ok = valueOfIter.Recv() {
out <- Pair{i, v.Interface()}
i++
}
}
示例5: pMapChanInt
func pMapChanInt(inChan reflect.Value, f interface{}, outChan reflect.Value) {
fVal := reflect.ValueOf(f)
val, ok := inChan.Recv()
for ok {
results := fVal.Call([]reflect.Value{val})
outChan.Send(results[0])
val, ok = inChan.Recv()
}
outChan.Close()
}
示例6: fromChan
func (t *Transport) fromChan(cid uint64, cval reflect.Value) error {
// Type check! woo
if cval.Kind() != reflect.Chan {
return fmt.Errorf("fatchan: cannot connect a %s - must be a channel", cval.Type())
}
if cval.Type().ChanDir()&reflect.RecvDir == 0 {
return fmt.Errorf("fatchan: cannot connect a %s - send-only channel", cval.Type())
}
sid := t.sid
go func() {
buf := new(bytes.Buffer)
for {
// Keep reusing the same buffer
buf.Reset()
// Wait for an object from the channel
v, ok := cval.Recv()
if !ok {
// send close message
t.writeBuf(cid, buf)
return
}
// Encode the object
if err := t.encodeValue(buf, v); err != nil {
t.err(sid, cid, err)
continue
}
// Send the encoding
if err := t.writeBuf(cid, buf); err != nil {
t.err(sid, cid, err)
break
}
}
// Drain the channel to close
for {
v, ok := cval.Recv()
if !ok {
break
}
t.err(sid, cid, fmt.Errorf("discarding %+v - channel closed due to error", v))
}
// Send EOF
t.err(sid, cid, io.EOF)
}()
return nil
}
示例7: chPublish
// Publish all values that arrive on the channel until it is closed or we
// ecounter an error.
func chPublish(c *EncodedConn, chVal reflect.Value, subject string) {
for {
val, ok := chVal.Recv()
if !ok {
// Channel has most likely been closed.
return
}
if e := c.Publish(subject, val.Interface()); e != nil {
if c.Conn.Opts.AsyncErrorCB != nil {
// FIXME(dlc) - Not sure this is the right thing to do.
go c.Conn.Opts.AsyncErrorCB(c.Conn, nil, e)
}
return
}
}
}
示例8: ConnectTypedWriteChannelToRaw
func ConnectTypedWriteChannelToRaw(writeChan reflect.Value, c chan []byte, wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer wg.Done()
var t reflect.Value
for ok := true; ok; {
if t, ok = writeChan.Recv(); ok {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.EncodeValue(t); err != nil {
log.Fatal("data type:", t.Type().String(), " ", t.Kind(), " encode error:", err)
}
c <- buf.Bytes()
}
}
close(c)
}()
}
示例9: reduceChan
func reduceChan(c reflect.Value, seed, f interface{}) (r interface{}, ok bool) {
switch f := f.(type) {
case func(interface{}, interface{}) interface{}:
v := reflect.New(c.Type().Elem()).Elem()
v.Set(reflect.ValueOf(seed))
for {
if x, open := c.Recv(); open {
v = reflect.ValueOf(f(v.Interface(), x))
} else {
break
}
}
r = v.Interface()
ok = true
default:
if f := reflect.ValueOf(f); f.Kind() == reflect.Func {
v := reflect.New(c.Type().Elem()).Elem()
v.Set(reflect.ValueOf(seed))
switch f.Type().NumIn() {
case 2:
for {
if x, open := c.Recv(); open {
v = f.Call([]reflect.Value{v, x})[0]
} else {
break
}
}
ok = true
}
if ok {
r = v.Interface()
}
}
}
return
}
示例10: eachChannel
func eachChannel(enum *Enumerator, c R.Value) (i int) {
var forEachReceived func(func(R.Value))
var x R.Value
var open bool
if enum.Span == 1 {
forEachReceived = func(f func(R.Value)) {
for {
if x, open = c.Recv(); !open {
PanicWithIndex(enum.cursor)
}
f(x)
i++
}
enum.cursor++
}
} else {
forEachReceived = func(f func(R.Value)) {
var offset int
for {
for offset = enum.Span; offset > 0; offset-- {
if x, open = c.Recv(); !open {
PanicWithIndex(enum.cursor + enum.Span)
}
}
f(x)
i++
}
enum.cursor += enum.Span
}
}
switch f := enum.f.(type) {
case func(interface{}):
forEachReceived(func(v R.Value) {
f(v.Interface())
})
case func(int, interface{}):
forEachReceived(func(v R.Value) {
f(enum.cursor, v.Interface())
})
case func(interface{}, interface{}):
forEachReceived(func(v R.Value) {
f(enum.cursor, v.Interface())
})
case func(R.Value):
forEachReceived(func(v R.Value) {
f(v)
})
case func(int, R.Value):
forEachReceived(func(v R.Value) {
f(enum.cursor, v)
})
case func(interface{}, R.Value):
forEachReceived(func(v R.Value) {
f(enum.cursor, v)
})
case func(R.Value, R.Value):
forEachReceived(func(v R.Value) {
f(R.ValueOf(enum.cursor), v)
})
case chan interface{}:
forEachReceived(func(v R.Value) {
f <- v.Interface()
})
case chan R.Value:
forEachReceived(func(v R.Value) {
f <- v
})
// case []chan interface{}:
// case []chan R.Value:
default:
if f := R.ValueOf(f); f.Kind() == R.Func {
if t := f.Type(); !t.IsVariadic() {
switch t.NumIn() {
case 1: // f(v)
p := make([]R.Value, 1, 1)
forEachReceived(func(v R.Value) {
p[0] = v
f.Call(p)
})
case 2: // f(i, v)
p := make([]R.Value, 2, 2)
forEachReceived(func(v R.Value) {
p[0], p[1] = R.ValueOf(enum.cursor), v
f.Call(p)
})
default:
panic(UNHANDLED_ITERATOR)
}
}
} else {
panic(UNHANDLED_ITERATOR)
}
}
return
}
示例11: whileChannel
func whileChannel(c R.Value, r bool, f interface{}) (count int) {
switch f := f.(type) {
case func(interface{}) bool:
for v, open := c.Recv(); open && f(v.Interface()) == r; count++ {
v, open = c.Recv()
}
case func(int, interface{}) bool:
for v, open := c.Recv(); open && f(count, v.Interface()) == r; count++ {
v, open = c.Recv()
}
case func(interface{}, interface{}) bool:
for v, open := c.Recv(); open && f(count, v.Interface()) == r; count++ {
v, open = c.Recv()
}
case func(...interface{}) bool:
p := make([]interface{}, 0, 4)
for v, open := c.Recv(); open; {
p = append(p, v.Interface())
v, open = c.Recv()
}
if f(p...) == r {
count = 1
}
case func(bool, ...interface{}) int:
p := make([]interface{}, 0, 4)
for v, open := c.Recv(); open; {
p = append(p, v.Interface())
v, open = c.Recv()
}
count = f(r, p...)
case func(R.Value) bool:
for v, open := c.Recv(); open && f(v) == r; count++ {
v, open = c.Recv()
}
case func(int, R.Value) bool:
for v, open := c.Recv(); open && f(count, v) == r; count++ {
v, open = c.Recv()
}
case func(interface{}, R.Value) bool:
for v, open := c.Recv(); open && f(count, v) == r; count++ {
v, open = c.Recv()
}
case func(R.Value, R.Value) bool:
for v, open := c.Recv(); open && f(R.ValueOf(count), v) == r; count++ {
v, open = c.Recv()
}
case func(...R.Value) bool:
p := make([]R.Value, 0, 4)
for v, open := c.Recv(); open; {
p = append(p, v)
v, open = c.Recv()
}
if f(p...) == r {
count = 1
}
case func(bool, ...R.Value) int:
p := make([]R.Value, 0, 4)
for v, open := c.Recv(); open; {
p = append(p, v)
v, open = c.Recv()
}
count = f(r, p...)
default:
if f := R.ValueOf(f); f.Kind() == R.Func {
if t := f.Type(); t.IsVariadic() {
switch t.NumIn() {
case 1: // f(...v) bool
p := make([]R.Value, 0, 4)
i := 0
for v, open := c.Recv(); open; i++ {
p = append(p, v)
v, open = c.Recv()
}
if f.Call(p)[0].Bool() == r {
count = 1
}
case 2: // f(bool, ...v) int
p := make([]R.Value, 0, 4)
i := 0
for v, open := c.Recv(); open; i++ {
p = append(p, v)
v, open = c.Recv()
}
count = int(f.Call(p)[0].Int())
}
} else {
switch t.NumIn() {
//.........这里部分代码省略.........
示例12: whileChannel
func whileChannel(c R.Value, r bool, f interface{}) (count int) {
switch f := f.(type) {
case func(interface{}) bool:
for v, open := c.Recv(); open && f(v.Interface()) == r; count++ {
v, open = c.Recv()
}
case func(int, interface{}) bool:
for v, open := c.Recv(); open && f(count, v.Interface()) == r; count++ {
v, open = c.Recv()
}
case func(interface{}, interface{}) bool:
for v, open := c.Recv(); open && f(count, v.Interface()) == r; count++ {
v, open = c.Recv()
}
case func(R.Value) bool:
for v, open := c.Recv(); open && f(v) == r; count++ {
v, open = c.Recv()
}
case func(int, R.Value) bool:
for v, open := c.Recv(); open && f(count, v) == r; count++ {
v, open = c.Recv()
}
case func(interface{}, R.Value) bool:
for v, open := c.Recv(); open && f(count, v) == r; count++ {
v, open = c.Recv()
}
case func(R.Value, R.Value) bool:
for v, open := c.Recv(); open && f(R.ValueOf(count), v) == r; count++ {
v, open = c.Recv()
}
default:
if f := R.ValueOf(f); f.Kind() == R.Func {
if t := f.Type(); t.NumOut() == 1 {
switch t.NumIn() {
case 1: // f(v) bool
open := false
p := make([]R.Value, 1, 1)
for p[0], open = c.Recv(); open && f.Call(p)[0].Bool() == r; count++ {
p[0], open = c.Recv()
}
case 2: // f(i, v) bool
open := false
p := make([]R.Value, 2, 2)
p[0] = R.ValueOf(0)
for p[1], open = c.Recv(); open && f.Call(p)[0].Bool() == r; count++ {
p[0] = R.ValueOf(count)
p[1], open = c.Recv()
}
default:
panic(UNHANDLED_ITERATOR)
}
}
}
}
return
}
示例13: fromChan
func (t *Transport) fromChan(cval reflect.Value) (uint64, uint64, error) {
sid, cid := t.sid, atomic.AddUint64(&t.nextCID, 1)
// Type check! woo
if cval.Kind() != reflect.Chan {
return sid, cid, fmt.Errorf("fatchan: cannot connect a %s - must be a channel", cval.Type())
}
if cval.Type().ChanDir()&reflect.RecvDir == 0 {
return sid, cid, fmt.Errorf("fatchan: cannot connect a %s - send-only channel", cval.Type())
}
var raw [8]byte
send := func(buf *bytes.Buffer) error {
t.write.Lock()
defer t.write.Unlock()
// Send the cid
n := binary.PutUvarint(raw[:], cid)
if _, err := t.rwc.Write(raw[:n]); err != nil {
return err
}
// Send the length
n = binary.PutUvarint(raw[:], uint64(buf.Len()))
if _, err := t.rwc.Write(raw[:n]); err != nil {
return err
}
// Send the bytes
if _, err := io.Copy(t.rwc, buf); err != nil {
return err
}
return nil
}
go func() {
buf := new(bytes.Buffer)
for {
// Keep reusing the same buffer
buf.Reset()
// Wait for an object from the channel
v, ok := cval.Recv()
if !ok {
send(buf)
return
}
// Encode the object
if err := t.encodeValue(buf, v); err != nil {
t.err(sid, cid, err)
continue
}
// Send the encoding
if err := send(buf); err != nil {
t.err(sid, cid, err)
break
}
}
for {
v, ok := cval.Recv()
if !ok {
break
}
t.err(sid, cid, fmt.Errorf("discarding %+v - channel closed", v))
}
t.err(sid, cid, io.EOF)
}()
return sid, cid, nil
}
示例14: doRange
// Iterate over a range of values, issuing a callback for each one. The callback
// fn is expected to take two arguments (index/key, value pair) and return an
// error.
func doRange(t *testingT, v reflect.Value, fn interface{}) {
t = t.sub("internal doRange")
fnval := reflect.ValueOf(fn)
fntyp := fnval.Type()
if numin := fntyp.NumIn(); numin != 2 {
t.Fatalf("function of %d arguments %v", numin, fn)
} else if numout := fntyp.NumOut(); numout != 1 {
t.Fatalf("function of %d return values %v", numout, fn)
}
zero := reflect.Zero(fnval.Type().In(1))
var out reflect.Value
switch k := v.Kind(); k {
case reflect.Slice:
for i, n := 0, v.Len(); i < n; i++ {
ival, vval := reflect.ValueOf(i), v.Index(i)
arg := validValue(t.sub(sprintf("index %d", i)), vval, zero)
if !arg.IsValid() {
continue
}
out = fnval.Call([]reflect.Value{ival, arg})[0]
if !out.IsNil() {
t.Error(out.Interface())
break
}
}
case reflect.Map:
for _, kval := range v.MapKeys() {
vval := v.MapIndex(kval)
arg := validValue(t.sub(sprintf("index %v", kval.Interface())), vval, zero)
if !arg.IsValid() {
continue
}
out = fnval.Call([]reflect.Value{kval, arg})[0]
if !out.IsNil() {
t.Error(out.Interface())
break
}
}
case reflect.Chan:
var vval reflect.Value
var ok bool
for i := 0; true; i++ {
ival := reflect.ValueOf(i)
if vval, ok = v.Recv(); !ok {
break
}
arg := validValue(t.sub(sprintf("received value %d", i)), vval, zero)
if !arg.IsValid() {
continue
}
out = fnval.Call([]reflect.Value{ival, arg})[0]
if !out.IsNil() {
t.Error(out.Interface())
break
}
}
default:
t.Fatalf("unacceptable type for range %v", v.Type())
}
}
示例15: rangeChannel
func rangeChannel(c R.Value, f interface{}) (ok bool) {
switch f := f.(type) {
case func(interface{}):
for v, open := c.Recv(); open; {
f(v.Interface())
v, open = c.Recv()
}
ok = true
case func(int, interface{}):
i := 0
for v, open := c.Recv(); open; i++ {
f(i, v.Interface())
v, open = c.Recv()
}
ok = true
case func(interface{}, interface{}):
i := 0
for v, open := c.Recv(); open; i++ {
f(i, v.Interface())
v, open = c.Recv()
}
ok = true
case func(...interface{}):
p := make([]interface{}, 0, 4)
for v, open := c.Recv(); open; {
p = append(p, v.Interface())
v, open = c.Recv()
}
f(p...)
ok = true
case func(R.Value):
for v, open := c.Recv(); open; {
f(v)
v, open = c.Recv()
}
ok = true
case func(int, R.Value):
i := 0
for v, open := c.Recv(); open; i++ {
f(i, v)
v, open = c.Recv()
}
ok = true
case func(interface{}, R.Value):
i := 0
for v, open := c.Recv(); open; i++ {
f(i, v)
v, open = c.Recv()
}
ok = true
case func(...R.Value):
p := make([]R.Value, 0, 4)
for v, open := c.Recv(); open; {
p = append(p, v)
v, open = c.Recv()
}
f(p...)
ok = true
default:
if f := R.ValueOf(f); f.Kind() == R.Func {
if t := f.Type(); t.IsVariadic() {
// f(...v)
p := make([]R.Value, 0, 4)
for v, open := c.Recv(); open; {
p = append(p, v)
v, open = c.Recv()
}
f.Call(p)
} else {
switch t.NumIn() {
case 1: // f(v)
p := make([]R.Value, 1, 1)
for v, open := c.Recv(); open; {
p[0] = v
f.Call(p)
v, open = c.Recv()
}
ok = true
case 2: // f(i, v)
p := make([]R.Value, 2, 2)
i := 0
for v, open := c.Recv(); open; i++ {
p[0], p[1] = R.ValueOf(i), v
f.Call(p)
v, open = c.Recv()
}
ok = true
}
}
}
}
//.........这里部分代码省略.........