當前位置: 首頁>>代碼示例>>Golang>>正文


Golang reflect.Typeof函數代碼示例

本文整理匯總了Golang中reflect.Typeof函數的典型用法代碼示例。如果您正苦於以下問題:Golang Typeof函數的具體用法?Golang Typeof怎麽用?Golang Typeof使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Typeof函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestKeyMappingInt64

func TestKeyMappingInt64(t *testing.T) {
	for i := 0; i < 10000; i++ {
		num := rand.Int63()
		if i%5 != 0 {
			num = -num
		}
		key, mask := sbitlocation(num)
		if bitcount(mask) != 1 {
			t.Errorf("Expected exactly one bit in mask; found %v", bitcount(mask))
		}
		dcnum := imemberval(key, getbits(mask)[0])
		switch tp := dcnum.(type) {
		case int64:
			if num >= 0 {
				t.Errorf("Expected type \"uint64\": got %v", reflect.Typeof(tp))
			}
			if num != tp {
				t.Errorf("Expected type %v: got %v (%v,%v)", num, tp, key, mask)
			}
		case uint64:
			if num < 0 {
				t.Errorf("Expected type \"int64\": got %v", reflect.Typeof(tp))
			}
			if uint64(num) != tp {
				t.Errorf("Expected type %v: got %v", num, tp)
			}
		default:
			t.Errorf("Expected type \"(u)int64\": got %v", reflect.Typeof(tp))
		}
	}
}
開發者ID:Mukaram,項目名稱:mudlark-go-pkgs,代碼行數:31,代碼來源:bitset_test.go

示例2: TestMakeSet

func TestMakeSet(t *testing.T) {
	set := New()
	if reflect.Typeof(set).String() != "*heteroset.Set" {
		t.Errorf("Expected type \"*heteroset.Set\": got %v", reflect.Typeof(set).String())
	}
	if set.Cardinality() != 0 {
		t.Errorf("Expected bitcount 0: got %v", set.Cardinality())
	}
	if set.root != nil {
		t.Errorf("Root is not nil")
	}
	has := set.Has(Int(1))
	if has {
		t.Errorf("Unexpectedly has Int")
	}
	if max_depth(set.root) != 0 {
		t.Errorf("Expected 0 max depth got: %v", max_depth(set.root))
	}
	has = set.Has(Real(1.0))
	if has {
		t.Errorf("Unexpectedly has Real")
	}
	if max_depth(set.root) != 0 {
		t.Errorf("Expected 0 max depth got: %v", max_depth(set.root))
	}
}
開發者ID:Mukaram,項目名稱:mudlark-go-pkgs,代碼行數:26,代碼來源:heteroset_test.go

示例3: init

func init() {
	var iop, uop decOp
	switch reflect.Typeof(int(0)).Bits() {
	case 32:
		iop = decInt32
		uop = decUint32
	case 64:
		iop = decInt64
		uop = decUint64
	default:
		panic("gob: unknown size of int/uint")
	}
	decOpMap[reflect.Int] = iop
	decOpMap[reflect.Uint] = uop

	// Finally uintptr
	switch reflect.Typeof(uintptr(0)).Bits() {
	case 32:
		uop = decUint32
	case 64:
		uop = decUint64
	default:
		panic("gob: unknown size of uintptr")
	}
	decOpMap[reflect.Uintptr] = uop
}
開發者ID:IntegerCompany,項目名稱:linaro-android-gcc,代碼行數:26,代碼來源:decode.go

示例4: newSysChans

func newSysChans(p *proxyImpl) *sysChans {
	sc := new(sysChans)
	sc.proxy = p
	r := p.router
	sc.pubSubInfo = make(chan *genericMsg, DefCmdChanBufSize)
	sc.sysRecvChans = newRecvChanBundle(p, ScopeLocal, MemberRemote)
	sc.sysSendChans = newSendChanBundle(p, ScopeLocal, MemberRemote)
	//
	pubSubChanType := reflect.Typeof(make(chan *IdChanInfoMsg)).(*reflect.ChanType)
	connChanType := reflect.Typeof(make(chan *ConnInfoMsg)).(*reflect.ChanType)
	readyChanType := reflect.Typeof(make(chan *ConnReadyMsg)).(*reflect.ChanType)

	sc.sysRecvChans.AddRecver(r.SysID(PubId), newGenericMsgChan(r.SysID(PubId), sc.pubSubInfo, false), -1 /*no flow control*/)
	sc.sysRecvChans.AddRecver(r.SysID(UnPubId), newGenericMsgChan(r.SysID(UnPubId), sc.pubSubInfo, false), -1 /*no flow control*/)
	sc.sysRecvChans.AddRecver(r.SysID(SubId), newGenericMsgChan(r.SysID(SubId), sc.pubSubInfo, false), -1 /*no flow control*/)
	sc.sysRecvChans.AddRecver(r.SysID(UnSubId), newGenericMsgChan(r.SysID(UnSubId), sc.pubSubInfo, false), -1 /*no flow control*/)

	sc.sysSendChans.AddSender(r.SysID(ConnId), connChanType)
	sc.sysSendChans.AddSender(r.SysID(DisconnId), connChanType)
	sc.sysSendChans.AddSender(r.SysID(ErrorId), connChanType)
	sc.sysSendChans.AddSender(r.SysID(ReadyId), readyChanType)
	sc.sysSendChans.AddSender(r.SysID(PubId), pubSubChanType)
	sc.sysSendChans.AddSender(r.SysID(UnPubId), pubSubChanType)
	sc.sysSendChans.AddSender(r.SysID(SubId), pubSubChanType)
	sc.sysSendChans.AddSender(r.SysID(UnSubId), pubSubChanType)
	return sc
}
開發者ID:niltonkummer,項目名稱:go-router,代碼行數:27,代碼來源:proxy.go

示例5: ImportNValues

// ImportNValues imports a channel of the given type and specified direction
// and then receives or transmits up to n values on that channel.  A value of
// n==0 implies an unbounded number of values.  The channel to be bound to
// the remote site's channel is provided in the call and may be of arbitrary
// channel type.
// Despite the literal signature, the effective signature is
//	ImportNValues(name string, chT chan T, dir Dir, pT T)
// where T must be a struct, pointer to struct, etc.  pT may be more indirect
// than the value type of the channel (e.g.  chan T, pT *T) but it must be a
// pointer.
// Example usage:
//	imp, err := NewImporter("tcp", "netchanserver.mydomain.com:1234")
//	if err != nil { log.Exit(err) }
//	ch := make(chan myType)
//	err := imp.ImportNValues("name", ch, Recv, new(myType), 1)
//	if err != nil { log.Exit(err) }
//	fmt.Printf("%+v\n", <-ch)
// (TODO: Can we eliminate the need for pT?)
func (imp *Importer) ImportNValues(name string, chT interface{}, dir Dir, pT interface{}, n int) os.Error {
	ch, err := checkChan(chT, dir)
	if err != nil {
		return err
	}
	// Make sure pT is a pointer (to a pointer...) to a struct.
	rt := reflect.Typeof(pT)
	if _, ok := rt.(*reflect.PtrType); !ok {
		return os.ErrorString("not a pointer:" + rt.String())
	}
	if _, ok := reflect.Indirect(reflect.NewValue(pT)).(*reflect.StructValue); !ok {
		return os.ErrorString("not a pointer to a struct:" + rt.String())
	}
	imp.chanLock.Lock()
	defer imp.chanLock.Unlock()
	_, present := imp.chans[name]
	if present {
		return os.ErrorString("channel name already being imported:" + name)
	}
	ptr := reflect.MakeZero(reflect.Typeof(pT)).(*reflect.PtrValue)
	imp.chans[name] = &importChan{ch, dir, ptr, n}
	// Tell the other side about this channel.
	req := new(request)
	req.name = name
	req.dir = dir
	req.count = n
	if err := imp.encode(req, nil); err != nil {
		log.Stderr("importer request encode:", err)
		return err
	}
	return nil
}
開發者ID:lougxing,項目名稱:golang-china,代碼行數:50,代碼來源:import.go

示例6: main

func main() {
	// check that reflect paths are correct,
	// meaning that reflect data for v0, v1 didn't get confused.

	// path is full (rooted) path name.  check suffix for gc, prefix for gccgo
	if s := reflect.Typeof(v0).PkgPath(); !strings.HasSuffix(s, "/bug0") && !strings.HasPrefix(s, "bug0") {
		panicln("bad v0 path", len(s), s)
	}
	if s := reflect.Typeof(v1).PkgPath(); !strings.HasSuffix(s, "/bug1") && !strings.HasPrefix(s, "bug1") {
		panicln("bad v1 path", s)
	}

	// check that dynamic interface check doesn't get confused
	var i interface{} = t0(0)
	if _, ok := i.(I1); ok {
		panicln("used t0 as i1")
	}
	if _, ok := i.(p1.I); ok {
		panicln("used t0 as p1.I")
	}

	i = t1(1)
	if _, ok := i.(I0); ok {
		panicln("used t1 as i0")
	}
	if _, ok := i.(p0.I); ok {
		panicln("used t1 as p0.I")
	}

	// check that type switch works.
	// the worry is that if p0.T and p1.T have the same hash,
	// the binary search will handle one of them incorrectly.
	for j := 0; j < 3; j++ {
		switch j {
		case 0:
			i = p0.T{}
		case 1:
			i = p1.T{}
		case 2:
			i = 3.14
		}
		switch k := i.(type) {
		case p0.T:
			if j != 0 {
				panicln("type switch p0.T")
			}
		case p1.T:
			if j != 1 {
				panicln("type switch p1.T")
			}
		default:
			if j != 2 {
				panicln("type switch default", j)
			}
		}
	}
}
開發者ID:ivanwyc,項目名稱:google-go,代碼行數:57,代碼來源:bug2.go

示例7: init

func init() {
	kindEncoder = map[reflect.Kind]encoderFunc{
		reflect.Array:     encodeArrayOrSlice,
		reflect.Bool:      encodeBool,
		reflect.Float32:   encodeFloat,
		reflect.Float64:   encodeFloat,
		reflect.Int32:     encodeInt,
		reflect.Int64:     func(e *encodeState, name string, value reflect.Value) { encodeInt64(e, kindInt64, name, value) },
		reflect.Int:       encodeInt,
		reflect.Interface: encodeInterfaceOrPtr,
		reflect.Map:       encodeMap,
		reflect.Ptr:       encodeInterfaceOrPtr,
		reflect.Slice:     encodeArrayOrSlice,
		reflect.String:    func(e *encodeState, name string, value reflect.Value) { encodeString(e, kindString, name, value) },
		reflect.Struct:    encodeStruct,
	}
	typeEncoder = map[reflect.Type]encoderFunc{
		typeDoc:                         encodeDoc,
		typeBSONData:                    encodeBSONData,
		reflect.Typeof(Code("")):        func(e *encodeState, name string, value reflect.Value) { encodeString(e, kindCode, name, value) },
		reflect.Typeof(CodeWithScope{}): encodeCodeWithScope,
		reflect.Typeof(DateTime(0)):     func(e *encodeState, name string, value reflect.Value) { encodeInt64(e, kindDateTime, name, value) },
		reflect.Typeof(MinMax(0)):       encodeMinMax,
		reflect.Typeof(ObjectId{}):      encodeObjectId,
		reflect.Typeof(Regexp{}):        encodeRegexp,
		reflect.Typeof(Symbol("")):      func(e *encodeState, name string, value reflect.Value) { encodeString(e, kindSymbol, name, value) },
		reflect.Typeof(Timestamp(0)):    func(e *encodeState, name string, value reflect.Value) { encodeInt64(e, kindTimestamp, name, value) },
		reflect.Typeof([]byte{}):        encodeByteSlice,
	}
}
開發者ID:hokapoka,項目名稱:go-mongo,代碼行數:30,代碼來源:bson_encode.go

示例8: TestConversion

func TestConversion(t *testing.T) {
	var cs *cloud2dStr
	if err := xml.Unmarshal(strings.NewReader(test_aida_string), &cs); err != nil {
		t.Errorf("xml.Unmarshal: %q", err)
	}
	c := cs.Convert()
	fmt.Println(reflect.Typeof(cs))
	fmt.Println(reflect.Typeof(c))
	fmt.Println(reflect.Typeof(cs).Name())
	fmt.Println(reflect.Typeof(c).Name())
}
開發者ID:jstrube,項目名稱:goida,代碼行數:11,代碼來源:Cloud2D_test.go

示例9: cmp_type

func cmp_type(a, b interface{}) int {
	ta := reflect.Typeof(a)
	tb := reflect.Typeof(b)
	if ta == tb {
		return 0
	}
	if cp := cmp_string(ta.PkgPath(), tb.PkgPath()); cp != 0 {
		return cp
	}
	return cmp_string(ta.Name(), tb.Name())
}
開發者ID:Mukaram,項目名稱:mudlark-go-pkgs,代碼行數:11,代碼來源:heteroset.go

示例10: TestMakeSetWithMembers

func TestMakeSetWithMembers(t *testing.T) {
	set := Make(-1, 28, 18, 28, 9)
	if reflect.Typeof(set).String() != "*bitset.Set" {
		t.Errorf("Expected type \"*bitset.Set\": got %v", reflect.Typeof(set).String())
	}
	if set.bitcount != 4 {
		t.Errorf("Expected bitcount 4: got %v", set.bitcount)
	}
	if set.bits == nil {
		t.Errorf("Bit map unitialized")
	}
}
開發者ID:Mukaram,項目名稱:mudlark-go-pkgs,代碼行數:12,代碼來源:bitset_test.go

示例11: TestReregistration

// Reregister some basic types to check registration is idempotent.
func TestReregistration(t *testing.T) {
	newtyp := getTypeUnlocked("int", reflect.Typeof(int(0)))
	if newtyp != tInt.gobType() {
		t.Errorf("reregistration of %s got new type", newtyp.string())
	}
	newtyp = getTypeUnlocked("uint", reflect.Typeof(uint(0)))
	if newtyp != tUint.gobType() {
		t.Errorf("reregistration of %s got new type", newtyp.string())
	}
	newtyp = getTypeUnlocked("string", reflect.Typeof("hello"))
	if newtyp != tString.gobType() {
		t.Errorf("reregistration of %s got new type", newtyp.string())
	}
}
開發者ID:GNA-SERVICES-INC,項目名稱:MoNGate,代碼行數:15,代碼來源:type_test.go

示例12: init

func init() {
	kindDecoder = map[reflect.Kind]decoderFunc{
		reflect.Bool:      decodeBool,
		reflect.Float32:   decodeFloat,
		reflect.Float64:   decodeFloat,
		reflect.Int32:     decodeInt,
		reflect.Int64:     decodeInt,
		reflect.Int:       decodeInt,
		reflect.Interface: decodeInterface,
		reflect.Map:       decodeMap,
		reflect.String:    decodeString,
		reflect.Struct:    decodeStruct,
		reflect.Slice:     decodeSlice,
		reflect.Array:     decodeArray,
	}
	typeDecoder = map[reflect.Type]decoderFunc{
		reflect.Typeof(BSONData{}):                   decodeBSONData,
		reflect.Typeof(DateTime(0)):                  decodeDateTime,
		reflect.Typeof(MinMax(0)):                    decodeMinMax,
		reflect.Typeof(ObjectId{}):                   decodeObjectId,
		reflect.Typeof(Symbol("")):                   decodeString,
		reflect.Typeof(Timestamp(0)):                 decodeTimestamp,
		reflect.Typeof([]byte{}):                     decodeByteSlice,
		reflect.Typeof(make(map[string]interface{})): decodeMapStringInterface,
	}
}
開發者ID:hokapoka,項目名稱:go-mongo,代碼行數:26,代碼來源:bson_decode.go

示例13: TestEndToEnd

func TestEndToEnd(t *testing.T) {
	type T2 struct {
		t string
	}
	s1 := "string1"
	s2 := "string2"
	type T1 struct {
		a, b, c int
		n       *[3]float
		strs    *[2]string
		int64s  *[]int64
		s       string
		y       []byte
		t       *T2
	}
	t1 := &T1{
		a:      17,
		b:      18,
		c:      -5,
		n:      &[3]float{1.5, 2.5, 3.5},
		strs:   &[2]string{s1, s2},
		int64s: &[]int64{77, 89, 123412342134},
		s:      "Now is the time",
		y:      strings.Bytes("hello, sailor"),
		t:      &T2{"this is T2"},
	}
	b := new(bytes.Buffer)
	encode(b, t1)
	var _t1 T1
	decode(b, getTypeInfoNoError(reflect.Typeof(_t1)).id, &_t1)
	if !reflect.DeepEqual(t1, &_t1) {
		t.Errorf("encode expected %v got %v", *t1, _t1)
	}
}
開發者ID:edisonwsk,項目名稱:golang-on-cygwin,代碼行數:34,代碼來源:codec_test.go

示例14: init

func init() {
	sampleValues = []interface{}{
		true,
		false,
		int(42), int8(42), int16(42), int32(42), int64(42),
		uint(42), uint8(42), uint16(42), uint32(42), uint64(42),
		float32(42), float64(42),
		complex(42, 42), complex64(42), complex128(42),
		"42",
		struct{ Field int }{Field: 42},
		&struct{ Field int }{Field: 42},
		reflect.Typeof(struct{ Field int }{Field: 42}),
		make(chan int, 42),
		func() int { return 42 },
		map[string]int{"forty": 40, "two": 2, "forty-two": 42},
		[]int{40, 41, 42},
		[...]int{40, 41, 42},
		[42]int{2: 40, 40: 2},
		nil,
		uninitialized,
		uninitialized._chan,
		uninitialized._func,
		uninitialized._interface,
		uninitialized._map,
		uninitialized._pointer,
		uninitialized._slice,
	}
}
開發者ID:rdrdr,項目名稱:hamcrest,代碼行數:28,代碼來源:core_test.go

示例15: printItems

func printItems(m Dict) {
	typ := reflect.Typeof(m)
	fmt.Printf("Dumping map(type=%s)...\n", typ.String())
	for item := range m.Iter() {
		fmt.Printf("%s: %d\n", item.key, item.val.(int))
	}
}
開發者ID:dmuir,項目名稱:functional-go,代碼行數:7,代碼來源:trie_test.go


注:本文中的reflect.Typeof函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。