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


Golang Type.Size方法代碼示例

本文整理匯總了Golang中reflect.Type.Size方法的典型用法代碼示例。如果您正苦於以下問題:Golang Type.Size方法的具體用法?Golang Type.Size怎麽用?Golang Type.Size使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在reflect.Type的用法示例。


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

示例1: sizeof

func sizeof(v reflect.Type) int {
	switch t := v.(type) {
	case *reflect.ArrayType:
		n := sizeof(t.Elem())
		if n < 0 {
			return -1
		}
		return t.Len() * n

	case *reflect.StructType:
		sum := 0
		for i, n := 0, t.NumField(); i < n; i++ {
			s := sizeof(t.Field(i).Type)
			if s < 0 {
				return -1
			}
			sum += s
		}
		return sum

	case *reflect.UintType, *reflect.IntType, *reflect.FloatType, *reflect.ComplexType:
		switch t := t.Kind(); t {
		case reflect.Int, reflect.Uint, reflect.Uintptr:
			return -1
		}
		return int(v.Size())
	}
	return -1
}
開發者ID:richlowe,項目名稱:gcc,代碼行數:29,代碼來源:binary.go

示例2: defaultType

func defaultType(t reflect.Type) TypeDesc {
	switch t.Kind() {
	case reflect.Bool:
		return BooleanType
	case reflect.String:
		return UTF8Type
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return LongType
	case reflect.Float32:
		return FloatType
	case reflect.Float64:
		return DoubleType
	case reflect.Array:
		if t.Name() == "UUID" && t.Size() == 16 {
			return UUIDType
		}
		return UnknownType
	case reflect.Struct:
		if t.Name() == "Time" && t.PkgPath() == "time" {
			return DateType
		}
		return UnknownType
	case reflect.Slice:
		if et := t.Elem(); et.Kind() == reflect.Uint8 {
			return BytesType
		}
		return UnknownType
	}
	return UnknownType
}
開發者ID:ngmoco,項目名稱:gossie,代碼行數:30,代碼來源:types.go

示例3: indexer

func indexer(t reflect.Type) indexFunc {
	size := t.Size()
	return func(v handle, i int) handle {
		header := (*reflect.SliceHeader)(unsafe.Pointer(&v))
		return handle(header.Data + uintptr(i)*size)
	}
}
開發者ID:rainycape,項目名稱:gondola,代碼行數:7,代碼來源:funcs_unsafe.go

示例4: sizeof

func sizeof(t reflect.Type) (int, error) {
	switch t.Kind() {
	case reflect.Array:
		n, err := sizeof(t.Elem())
		if err != nil {
			return 0, err
		}
		return t.Len() * n, nil

	case reflect.Struct:
		sum := 0
		for i, n := 0, t.NumField(); i < n; i++ {
			s, err := sizeof(t.Field(i).Type)
			if err != nil {
				return 0, err
			}
			sum += s
		}
		return sum, nil

	case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
		return int(t.Size()), nil
	}
	return 0, errors.New("invalid type " + t.String())
}
開發者ID:h8liu,項目名稱:golang,代碼行數:27,代碼來源:binary.go

示例5: integerPromote

// integerPromote determines if we can promote v to dType, and if so, return the promoted value.
// This is needed because msgpack always encodes values as the minimum sized int that can hold them.
func integerPromote(dType reflect.Type, v reflect.Value) (reflect.Value, bool) {

	vt := v.Type()
	dsz := dType.Size()
	vtsz := vt.Size()

	if isIntType(dType) && isIntType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetInt(v.Int())
		return pv, true
	}

	if isUintType(dType) && isUintType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetUint(v.Uint())
		return pv, true
	}

	if isIntType(dType) && isUintType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetInt(int64(v.Uint()))
		return pv, true
	}

	if isUintType(dType) && isIntType(vt) && vtsz <= dsz {
		pv := reflect.New(dType).Elem()
		pv.SetUint(uint64(v.Int()))
		return pv, true
	}

	return v, false
}
開發者ID:andrebq,項目名稱:skynet,代碼行數:34,代碼來源:rpc.go

示例6: sizeof

// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
func sizeof(t reflect.Type) int {
	switch t.Kind() {
	case reflect.Array:
		if s := sizeof(t.Elem()); s >= 0 {
			return s * t.Len()
		}

	case reflect.Struct:
		sum := 0
		for i, n := 0, t.NumField(); i < n; i++ {
			s := sizeof(t.Field(i).Type)
			if s < 0 {
				return -1
			}
			sum += s
		}
		return sum

	case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Ptr:
		return int(t.Size())
	}

	return -1
}
開發者ID:freedomkk-qfeng,項目名稱:agent,代碼行數:27,代碼來源:binary.go

示例7: benchmarkWriteSlice

func benchmarkWriteSlice(b *testing.B, typ reflect.Type, count int) {
	slice := reflect.MakeSlice(reflect.SliceOf(typ), count, count).Interface()
	b.SetBytes(int64(count * int(typ.Size())))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Write(ioutil.Discard, BigEndian, slice)
	}
}
開發者ID:rainycape,項目名稱:gondola,代碼行數:8,代碼來源:binary_test.go

示例8: benchmarkReadSlice

func benchmarkReadSlice(b *testing.B, typ reflect.Type, count int) {
	fr := &fakeReader{}
	slice := reflect.MakeSlice(reflect.SliceOf(typ), count, count).Interface()
	b.SetBytes(int64(count * int(typ.Size())))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Read(fr, BigEndian, slice)
	}
}
開發者ID:rainycape,項目名稱:gondola,代碼行數:9,代碼來源:binary_test.go

示例9: structMatchGeneric

func structMatchGeneric(t *testing.T, ta, tb reflect.Type, sza, szb uintptr) {
	if sza != szb {
		t.Errorf("type size mismatch: %s(%d) != %s(%d)",
			ta.Name(), ta.Size(), tb.Name(), tb.Size())
		t.Fail()
	}

	if ta.Kind() != tb.Kind() {
		t.Errorf("type kind mismatch: %s=%s != %s=%s",
			ta.Name(), ta.Kind(), tb.Name(), tb.Kind())
		t.Fail()
	}
}
開發者ID:veandco,項目名稱:go-sdl2,代碼行數:13,代碼來源:struct_test.go

示例10: sse2Reflect

func sse2Reflect(t reflect.Type) sse2type {
	elemSize := uint(0)
	if t.Kind() == reflect.Array {
		elemSize = uint(t.Elem().Size())
	}
	return sse2type{
		name:     t.Name(),
		size:     uint(t.Size()),
		elemSize: elemSize,
		align:    uint(t.Size()),
		t:        t,
	}
}
開發者ID:bjwbell,項目名稱:gensimd,代碼行數:13,代碼來源:sizeinfo.go

示例11: indexSetter

func indexSetter(t reflect.Type) indexSetFunc {
	size := t.Size()
	return func(v handle, i int, val handle) {
		header := (*reflect.SliceHeader)(unsafe.Pointer(&v))
		var dst []byte
		dh := (*reflect.SliceHeader)(unsafe.Pointer(&dst))
		dh.Len = int(size)
		dh.Data = header.Data + uintptr(i)*size
		var src []byte
		sh := (*reflect.SliceHeader)(unsafe.Pointer(&src))
		sh.Len = int(size)
		sh.Data = uintptr(val)
		copy(dst, src)
	}
}
開發者ID:rainycape,項目名稱:gondola,代碼行數:15,代碼來源:funcs_unsafe.go

示例12: MatchHeaders

func MatchHeaders(b reflect.Type, slice interface{}, header reflect.SliceHeader) (e error) {
	h, s, al := SliceHeader(slice)
	switch {
	case s != int(b.Size()):
		e = HeaderMismatch{"%v: slice element size should be %v not %v", b.Name(), b.Size(), s}
	case al != b.Align():
		e = HeaderMismatch{"%v: slice element alignment should be %v not %v", b.Name(), b.Align(), al}
	case h.Data != header.Data:
		e = HeaderMismatch{"%v: slice headers should point to the same memory: %v - %v", b.Name(), h.Data, header.Data}
	case h.Len != header.Len:
		e = HeaderMismatch{"%v: slice header lengths should be the same: %v - %v", b.Name(), h.Len, header.Len}
	case h.Cap != header.Cap:
		e = HeaderMismatch{"%v: slice header capacities should be the same: %v - %v", b.Name(), h.Cap, header.Cap}
	}
	return
}
開發者ID:feyeleanor,項目名稱:raw,代碼行數:16,代碼來源:reslice_test.go

示例13: swapper

func swapper(t reflect.Type) swapFunc {
	size := t.Size()
	tmp := make([]byte, size)
	return func(v handle, i, j int) {
		header := (*reflect.SliceHeader)(unsafe.Pointer(&v))
		var si []byte
		hi := (*reflect.SliceHeader)(unsafe.Pointer(&si))
		hi.Len = int(size)
		hi.Data = header.Data + uintptr(i)*size
		var sj []byte
		hj := (*reflect.SliceHeader)(unsafe.Pointer(&sj))
		hj.Len = int(size)
		hj.Data = header.Data + uintptr(j)*size
		copy(tmp, sj)
		copy(sj, si)
		copy(si, tmp)
	}
}
開發者ID:rainycape,項目名稱:gondola,代碼行數:18,代碼來源:funcs_unsafe.go

示例14: NewMesh

func NewMesh(attrs interface{}, indicies []uint32) *Mesh {
	mesh := new(Mesh)
	mesh.Attrs = attrs
	mesh.Indicies = indicies
	gl33.GenVertexArrays(1, &mesh.vao)
	gl33.BindVertexArray(mesh.vao)
	attrsValue := reflect.ValueOf(attrs)
	mesh.vertexBO = NewBuffer(gl33.ARRAY_BUFFER, gl33.STATIC_DRAW, attrs)
	vertexSpec := attrsValue.Type().Elem()
	if vertexSpec.Kind() != reflect.Struct && vertexSpec.Kind() != reflect.Array {
		panic("attrs is not a slice of structs or arrays")
	}
	var num int
	if vertexSpec.Kind() == reflect.Struct {
		num = vertexSpec.NumField()
	} else {
		num = vertexSpec.Len()
	}
	for i, offset := 0, uintptr(0); i < num; i++ {
		var field reflect.Type
		var type_ gl33.Enum
		var dimensions int
		if vertexSpec.Kind() == reflect.Struct {
			field = vertexSpec.Field(i).Type
		} else {
			field = vertexSpec.Elem()
		}
		if field.Kind() == reflect.Array {
			type_ = glType(field.Elem().Kind())
			dimensions = field.Len()
		} else {
			type_ = glType(field.Kind())
			dimensions = 1
		}
		setupAttrib(gl33.Uint(i), type_, dimensions, offset, int(vertexSpec.Size()))
		offset += field.Size()
	}
	mesh.indexBO = NewBuffer(gl33.ELEMENT_ARRAY_BUFFER, gl33.STATIC_DRAW, indicies)
	return mesh
}
開發者ID:klkblake,項目名稱:sge,代碼行數:40,代碼來源:mesh.go

示例15: structMatch

func structMatch(t *testing.T, ta, tb reflect.Type) {
	structMatchGeneric(t, ta, tb, ta.Size(), tb.Size())
}
開發者ID:veandco,項目名稱:go-sdl2,代碼行數:3,代碼來源:struct_test.go


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