Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is g struct define in golang source code?

Tags:

go

In golang source code, there is some code like:

MOVL    (g_sched+gobuf_sp)(SI), SP  // sp = m->g0->sched.sp

And in runtime/stubs.go I found

func getg() *g

But i can not find getg function or g struct, where is it?

like image 510
gwtony Avatar asked Jan 22 '26 20:01

gwtony


1 Answers

The struct is in runtime2.go.

type g struct {
  // Stack parameters.
  // stack describes the actual stack memory: [stack.lo, stack.hi).
  // stackguard0 is the stack pointer compared in the Go stack growth prologue.

You can use godef to quickly answer questions like that on your own.

The function's comment describes how that works:

The compiler rewrites calls to this function into instructions that fetch the g directly (from TLS or from the dedicated register).

To see how the compiler does this, check cmd/compile/internal/gc/typecheck.go:

if ..... && n.Left.Sym.Name == "getg" { ...
like image 66
cnicutar Avatar answered Jan 25 '26 20:01

cnicutar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!