當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。