当前位置: 首页>>代码示例>>Golang>>正文


Golang reflect.ChanOf函数代码示例

本文整理汇总了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
}
开发者ID:upccup,项目名称:cuplearn,代码行数:26,代码来源:chanof.go

示例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)
}
开发者ID:Comdex,项目名称:go-martini-mgo-demo,代码行数:26,代码来源:inject_test.go

示例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))
}
开发者ID:rails0330,项目名称:martini-sockets,代码行数:11,代码来源:sockets.go

示例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()
}
开发者ID:vuleetu,项目名称:misc,代码行数:12,代码来源:channel.go

示例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)
	})
}
开发者ID:pulcy,项目名称:robin,代码行数:49,代码来源:inject_test.go

示例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")

	}
}
开发者ID:corvuscrypto,项目名称:gpoll,代码行数:34,代码来源:client_manager_test.go

示例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
}
开发者ID:nagyistge,项目名称:hm-workspace,代码行数:7,代码来源:chanreflect.go

示例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>
}
开发者ID:nagyistge,项目名称:hm-workspace,代码行数:7,代码来源:chanreflect.go

示例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
}
开发者ID:2722,项目名称:lantern,代码行数:7,代码来源:chanreflect.go

示例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()
}
开发者ID:hahan,项目名称:cockroach,代码行数:38,代码来源:local_db.go

示例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
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:40,代码来源:type-check.go

示例12: main

func main() {
	t := reflect.TypeOf(3)
	chtype := reflect.ChanOf(reflect.RecvDir, t)
	inst := *(reflect.New(chtype).Interface().(*<-chan int))

	fmt.Println(inst)
}
开发者ID:yandongxiao,项目名称:the-way-to-go_ZH_CN,代码行数:7,代码来源:new2.go

示例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()
}
开发者ID:paulbellamy,项目名称:pipe,代码行数:34,代码来源:flatten_chan.go

示例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
}
开发者ID:newday1,项目名称:go-underscore,代码行数:9,代码来源:underscore.go

示例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)

}
开发者ID:nemowen,项目名称:golang,代码行数:8,代码来源:testReflect1.go


注:本文中的reflect.ChanOf函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。