Entry point and startup code of a go program?

I am a C programmer and I know how a C program is compiled and linked into an exe file. For example, in Windows and MSVC, there is a mainCRTStartup function which is the entry point of an exe, this function resides in the C runtime library, and it prepares argc and argv, and then calls main.

For go, I have no idea how a go program is compiled and linked into an exe. What is the entry point of a go program? What does the entry or startup function do? Where do the startup codes reside? Can someone shed a light on me or point me to relevant resources? Thank you very much!

A main function executes by default when you run the main package, eg a file containing your main() function in the package main, e.g. main,go:

package main

func main() {
  // Everything starts here...
}

To compile and run your application:
#: go run main.go
To compile and build your application to an executable:
#: go build main.go

You need to use “go run .” if your main.go uses other go files in the same directory. There can only be one main() function as entry point.

The main() function is your startup function and only does what you tell it to do, that means you read and prepare args yourself here if needed. There’s also an optional init() function that gets called before main().

1 Like

or func init(){}

I think Yiping Cheng is asking about the actual loading process of a Go program under Windows.

Yes. The first two answers are not what I really want.