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


Golang unsafe.Offsetof函数代码示例

本文整理汇总了Golang中unsafe.Offsetof函数的典型用法代码示例。如果您正苦于以下问题:Golang Offsetof函数的具体用法?Golang Offsetof怎么用?Golang Offsetof使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Offsetof函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: testDeep

func testDeep() {
	var s1 S1
	switch {
	case unsafe.Offsetof(s1.A) != 0:
		panic("unsafe.Offsetof(s1.A) != 0")
	case unsafe.Offsetof(s1.B) != 4:
		panic("unsafe.Offsetof(s1.B) != 4")
	case unsafe.Offsetof(s1.C) != 8:
		panic("unsafe.Offsetof(s1.C) != 8")
	case unsafe.Offsetof(s1.D) != 12:
		panic("unsafe.Offsetof(s1.D) != 12")
	case unsafe.Offsetof(s1.E) != 16:
		panic("unsafe.Offsetof(s1.E) != 16")
	case unsafe.Offsetof(s1.F) != 20:
		panic("unsafe.Offsetof(s1.F) != 20")
	case unsafe.Offsetof(s1.G) != 24:
		panic("unsafe.Offsetof(s1.G) != 24")
	case unsafe.Offsetof(s1.H) != 28:
		panic("unsafe.Offsetof(s1.H) != 28")
	case unsafe.Offsetof(s1.S1) != 32:
		panic("unsafe.Offsetof(s1.S1) != 32")
	case unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 4:
		panic("unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 4")
	}
}
开发者ID:qunhu,项目名称:go_src_comment,代码行数:25,代码来源:sizeof.go

示例2: main

func main() {
	v := "12139a532f3c29d678d879135532843838"
	log.Println(v)
	log.Println([]byte(v))
	v2 := hex2bytes(v)
	log.Println(v2)
	log.Println([]byte(v2))

	log.Println(revert([]byte(v2)))

	log.Println("@sizeOf v:", unsafe.Sizeof(v))
	log.Println("@sizeOf v2:", unsafe.Sizeof(&v2))
	v3 := `// 定義一個 Student 的結構
type Student struct {
    name    string
    id      int
    grade   float32
    friends []string
}
// 宣告一個內容為空的 student 變數代表這個結構
student := Student{}`
	log.Println("@sizeOf v3:", unsafe.Sizeof(&v3))
	log.Println("@cap v:", cap([]byte(v)))
	log.Println("@cap v2:", cap([]byte(v2)))
	log.Println("@cap v3:", cap([]byte(v3)))
	s1 := &SH{v: v}
	s2 := &SH{v: v2}
	s3 := &SH{v: v3}
	log.Println("szo  s1:", unsafe.Sizeof(s1.v))
	log.Println("szo  s2:", unsafe.Sizeof(s2.v))
	log.Println("szo  s3:", unsafe.Sizeof(s3.v))
	log.Println("szo  s1:", unsafe.Offsetof(s1.v))
	log.Println("szo  s2:", unsafe.Offsetof(s2.v))
	log.Println("szo  s3:", unsafe.Offsetof(s3.v))
}
开发者ID:xiaotiejiang888,项目名称:goPraticse,代码行数:35,代码来源:smallHex.go

示例3: NewRenderer

func NewRenderer(bufferSize int) (r *Renderer, err error) {
	var (
		instance       renderInstance
		instanceStride = unsafe.Sizeof(instance)
	)
	r = &Renderer{
		shader:     core.NewProgram(),
		bufferSize: bufferSize,
		buffer:     make([]renderInstance, bufferSize),
		stride:     instanceStride,
	}
	if err = r.shader.Load(VERTEX, FRAGMENT); err != nil {
		return
	}
	r.shader.Bind()

	r.vbo = core.NewArrayBuffer()

	r.shader.Attrib("f_InstanceFrame", instanceStride).Float(unsafe.Offsetof(instance.frame), 1)
	r.shader.Attrib("m_Model", instanceStride).Mat4(unsafe.Offsetof(instance.model), 1)
	r.shader.Attrib("v_Color", instanceStride).Vec4(unsafe.Offsetof(instance.color), 1)

	r.textureData = r.shader.UniformBlock("TextureData", 1)

	r.uView = r.shader.Uniform("m_View")
	r.uProj = r.shader.Uniform("m_Projection")

	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR: OpenGL error %X", e)
	}
	return
}
开发者ID:kurrik,项目名称:opengl-benchmarks,代码行数:32,代码来源:renderer.go

示例4: main

func main() {
	example := &Example{
		BoolValue: true,
	}

	exampleNext := &Example{
		BoolValue: true,
	}

	alignmentBoundary := unsafe.Alignof(example)

	sizeBool := unsafe.Sizeof(example.BoolValue)
	offsetBool := unsafe.Offsetof(example.BoolValue)

	sizeBoolNext := unsafe.Sizeof(exampleNext.BoolValue)
	offsetBoolNext := unsafe.Offsetof(exampleNext.BoolValue)

	fmt.Printf("Alignment Boundary: %d\n", alignmentBoundary)

	fmt.Printf("BoolValue = Size: %d Offset: %d Addr: %v\n",
		sizeBool, offsetBool, &example.BoolValue)

	fmt.Printf("Next = Size: %d Offset: %d Addr: %v\n",
		sizeBoolNext, offsetBoolNext, &exampleNext.BoolValue)
}
开发者ID:sthapaun,项目名称:ex,代码行数:25,代码来源:memory-footprint2.go

示例5: main

// main is the entry point for the application.
func main() {
	var e example

	alignmentBoundary := unsafe.Alignof(e)
	sizeE := unsafe.Sizeof(e)

	sizeBool := unsafe.Sizeof(e.flag)
	offsetBool := unsafe.Offsetof(e.flag)

	sizeInt := unsafe.Sizeof(e.counter)
	offsetInt := unsafe.Offsetof(e.counter)

	sizeBool2 := unsafe.Sizeof(e.flag2)
	offsetBool2 := unsafe.Offsetof(e.flag2)

	sizeFloat := unsafe.Sizeof(e.pi)
	offsetFloat := unsafe.Offsetof(e.pi)

	fmt.Printf("Alignment(%d):\tSize: %d\n", alignmentBoundary, sizeE)

	fmt.Printf("flag:\t\tSize: %d\t\tOffset: %d\tAddr: %v\n",
		sizeBool, offsetBool, &e.flag)

	fmt.Printf("counter:\tSize: %d\t\tOffset: %d\tAddr: %v\n",
		sizeInt, offsetInt, &e.counter)

	fmt.Printf("flag1:\t\tSize: %d\t\tOffset: %d\tAddr: %v\n",
		sizeBool2, offsetBool2, &e.flag2)

	fmt.Printf("pi\t\tSize: %d\t\tOffset: %d\tAddr: %v\n",
		sizeFloat, offsetFloat, &e.pi)
}
开发者ID:JacobJohansen,项目名称:gotraining,代码行数:33,代码来源:example1.go

示例6: main

func main() {
	var s S
	println(unsafe.Offsetof(s.a))
	println(unsafe.Offsetof(s.b))
	println(unsafe.Offsetof(s.c))
	println(unsafe.Offsetof(s.d))
}
开发者ID:glycerine,项目名称:llgo,代码行数:7,代码来源:offsetof.go

示例7: NewLinesRenderer

func NewLinesRenderer(camera *Camera) (lr *LinesRenderer, err error) {
	var (
		program uint32
		vbos    = make([]uint32, 2)
		point   TexturedPoint
	)
	if program, err = BuildProgram(LINES_VERTEX, LINES_FRAGMENT); err != nil {
		return
	}
	gl.GenBuffers(2, &vbos[0])
	lr = &LinesRenderer{
		Renderer:      NewRenderer(camera),
		program:       program,
		buffer:        vbos[0],
		indexBuffer:   vbos[1],
		bufferBytes:   0,
		positionLoc:   uint32(gl.GetAttribLocation(program, gl.Str("v_Position\x00"))),
		normalLoc:     uint32(gl.GetAttribLocation(program, gl.Str("v_Normal\x00"))),
		miterLoc:      uint32(gl.GetAttribLocation(program, gl.Str("f_Miter\x00"))),
		modelviewLoc:  gl.GetUniformLocation(program, gl.Str("m_ModelView\x00")),
		projectionLoc: gl.GetUniformLocation(program, gl.Str("m_Projection\x00")),
		thicknessLoc:  gl.GetUniformLocation(program, gl.Str("f_Thickness\x00")),
		colorLoc:      gl.GetUniformLocation(program, gl.Str("v_Color\x00")),
		innerLoc:      gl.GetUniformLocation(program, gl.Str("f_Inner\x00")),
		offPosition:   gl.PtrOffset(int(unsafe.Offsetof(point.X))),
		offNormal:     gl.PtrOffset(int(unsafe.Offsetof(point.TextureX))),
		offMiter:      gl.PtrOffset(int(unsafe.Offsetof(point.Z))),
		stride:        int32(unsafe.Sizeof(point)),
	}
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR: OpenGL error %X", e)
	}
	return
}
开发者ID:pikkpoiss,项目名称:twodee,代码行数:34,代码来源:renderer_lines.go

示例8: attrOutSize

func attrOutSize(p Protocol) uintptr {
	switch {
	case p.LT(Protocol{7, 9}):
		return unsafe.Offsetof(attrOut{}.Attr) + unsafe.Offsetof(attrOut{}.Attr.Blksize)
	default:
		return unsafe.Sizeof(attrOut{})
	}
}
开发者ID:angelabier1,项目名称:pachyderm,代码行数:8,代码来源:fuse_kernel.go

示例9: f

func f() {
	defer int(0)             // ERROR "defer requires function call, not conversion"
	go string([]byte("abc")) // ERROR "go requires function call, not conversion"

	var c complex128
	var f float64
	var t struct{ X int }

	var x []int
	defer append(x, 1)         // ERROR "defer discards result of append"
	defer cap(x)               // ERROR "defer discards result of cap"
	defer complex(1, 2)        // ERROR "defer discards result of complex"
	defer complex(f, 1)        // ERROR "defer discards result of complex"
	defer imag(1i)             // ERROR "defer discards result of imag"
	defer imag(c)              // ERROR "defer discards result of imag"
	defer len(x)               // ERROR "defer discards result of len"
	defer make([]int, 1)       // ERROR "defer discards result of make"
	defer make(chan bool)      // ERROR "defer discards result of make"
	defer make(map[string]int) // ERROR "defer discards result of make"
	defer new(int)             // ERROR "defer discards result of new"
	defer real(1i)             // ERROR "defer discards result of real"
	defer real(c)              // ERROR "defer discards result of real"
	defer append(x, 1)         // ERROR "defer discards result of append"
	defer append(x, 1)         // ERROR "defer discards result of append"
	defer unsafe.Alignof(t.X)  // ERROR "defer discards result of unsafe.Alignof"
	defer unsafe.Offsetof(t.X) // ERROR "defer discards result of unsafe.Offsetof"
	defer unsafe.Sizeof(t)     // ERROR "defer discards result of unsafe.Sizeof"

	defer copy(x, x) // ok
	m := make(map[int]int)
	defer delete(m, 1) // ok
	defer panic(1)     // ok
	defer print(1)     // ok
	defer println(1)   // ok
	defer recover()    // ok

	int(0)                // ERROR "int\(0\) evaluated but not used"
	string([]byte("abc")) // ERROR "string\(.*\) evaluated but not used"

	append(x, 1)         // ERROR "not used"
	cap(x)               // ERROR "not used"
	complex(1, 2)        // ERROR "not used"
	complex(f, 1)        // ERROR "not used"
	imag(1i)             // ERROR "not used"
	imag(c)              // ERROR "not used"
	len(x)               // ERROR "not used"
	make([]int, 1)       // ERROR "not used"
	make(chan bool)      // ERROR "not used"
	make(map[string]int) // ERROR "not used"
	new(int)             // ERROR "not used"
	real(1i)             // ERROR "not used"
	real(c)              // ERROR "not used"
	append(x, 1)         // ERROR "not used"
	append(x, 1)         // ERROR "not used"
	unsafe.Alignof(t.X)  // ERROR "not used"
	unsafe.Offsetof(t.X) // ERROR "not used"
	unsafe.Sizeof(t)     // ERROR "not used"
}
开发者ID:qunhu,项目名称:go_src_comment,代码行数:58,代码来源:issue4654.go

示例10: Test_FieldsAligned

func Test_FieldsAligned(t *testing.T) {
	p1 := new(DB)
	testAligned(t, "DB.seq", unsafe.Offsetof(p1.seq))
	p2 := new(session)
	testAligned(t, "session.stFileNum", unsafe.Offsetof(p2.stFileNum))
	testAligned(t, "session.stJournalNum", unsafe.Offsetof(p2.stJournalNum))
	testAligned(t, "session.stPrevJournalNum", unsafe.Offsetof(p2.stPrevJournalNum))
	testAligned(t, "session.stSeq", unsafe.Offsetof(p2.stSeq))
}
开发者ID:stoiclabs,项目名称:blockchainr,代码行数:9,代码来源:db_test.go

示例11: Dimensions

func (b *TrackHeaderBoxVersion1) Dimensions(atomPosition int64) *Dimensions {
	return &Dimensions{
		Width:              b.Width,
		WidthPosition:      atomPosition + int64(unsafe.Offsetof(TrackHeaderBoxVersion1{}.Width)),
		Height:             b.Height,
		HeightPosition:     int64(unsafe.Offsetof(TrackHeaderBoxVersion1{}.Height)) + atomPosition,
		SwapHeightAndWidth: calculateSwapHeightAndWidth(b.Matrix),
	}
}
开发者ID:CollabraMusic,项目名称:mp4-proxy,代码行数:9,代码来源:dimensions.go

示例12: main

func main() {
	// Test unsafe.Sizeof, unsafe.Alignof, and unsafe.Offsetof all return uintptr.
	isUintptr(unsafe.Sizeof(t))
	isUintptr(unsafe.Alignof(t))
	isUintptr(unsafe.Offsetof(t.X))

	// Test correctness of Offsetof with respect to embedded fields (issue 4909).
	if unsafe.Offsetof(t2.C) != 8 {
		println(unsafe.Offsetof(t2.C), "!= 8")
		panic("unsafe.Offsetof(t2.C) != 8")
	}
	if unsafe.Offsetof(p2.C) != 8 {
		println(unsafe.Offsetof(p2.C), "!= 8")
		panic("unsafe.Offsetof(p2.C) != 8")
	}
	if unsafe.Offsetof(t2.U2.C) != 4 {
		println(unsafe.Offsetof(t2.U2.C), "!= 4")
		panic("unsafe.Offsetof(t2.U2.C) != 4")
	}
	if unsafe.Offsetof(p2.U2.C) != 4 {
		println(unsafe.Offsetof(p2.U2.C), "!= 4")
		panic("unsafe.Offsetof(p2.U2.C) != 4")
	}
	testDeep()
	testNotEmbedded()
}
开发者ID:qunhu,项目名称:go_src_comment,代码行数:26,代码来源:sizeof.go

示例13: registerGeometry

func (r *Renderer) registerGeometry(geometry *Geometry) {
	var (
		pt       Point
		ptStride = unsafe.Sizeof(pt)
	)
	geometry.Bind()
	geometry.Upload()
	r.shader.Attrib("v_Position", ptStride).Vec3(unsafe.Offsetof(pt.Position), 0)
	r.shader.Attrib("v_Texture", ptStride).Vec2(unsafe.Offsetof(pt.Texture), 0)
	r.shader.Attrib("f_VertexFrame", ptStride).Float(unsafe.Offsetof(pt.Frame), 0)
}
开发者ID:kurrik,项目名称:opengl-benchmarks,代码行数:11,代码来源:renderer.go

示例14: init

func init() {
	var memStats MemStats
	if sizeof_C_MStats != unsafe.Sizeof(memStats) {
		println(sizeof_C_MStats, unsafe.Sizeof(memStats))
		throw("MStats vs MemStatsType size mismatch")
	}

	if unsafe.Offsetof(memstats.heap_live)%8 != 0 {
		println(unsafe.Offsetof(memstats.heap_live))
		throw("memstats.heap_live not aligned to 8 bytes")
	}
}
开发者ID:Harvey-OS,项目名称:go,代码行数:12,代码来源:mstats.go

示例15: FormHeapTuple

func FormHeapTuple(values []system.Datum, tupdesc *TupleDesc) *HeapTuple {
	natts := len(tupdesc.Attrs)
	hasnull := false
	for _, value := range values {
		if value == nil {
			hasnull = true
			//} else if att.attlen == -1
			// TODO: flatten toast value
		}
	}

	length := unsafe.Offsetof(HeapTupleHeader{}.bits)

	if hasnull {
		length += uintptr(bitmapLength(natts))
	}
	if tupdesc.hasOid {
		length += unsafe.Sizeof(system.Oid(0))
	}

	length = system.MaxAlign(length)
	hoff := uint8(length)

	data_len := computeHeapDataSize(values, tupdesc)
	length += data_len

	tuple_data := make([]byte, length)
	tuple := &HeapTuple{
		tupdesc:  tupdesc,
		tableOid: system.InvalidOid,
	}
	tuple.SetData(tuple_data, system.InvalidItemPointer)

	td := tuple.data
	td.SetNatts(system.AttrNumber(natts))
	td.hoff = hoff

	if tupdesc.hasOid {
		td.infomask = heapHasOid
	}

	bits := []byte(nil)
	if hasnull {
		bits = tuple.bytes[unsafe.Offsetof(td.bits):hoff]
	}
	data := tuple.bytes[hoff:]
	td.fill(values, tupdesc, bits, data)

	return tuple
}
开发者ID:umitanuki,项目名称:bigpot,代码行数:50,代码来源:tuple.go


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