当前位置: 首页>>代码示例>>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;未经允许,请勿转载。