Skip to main content TerryFunggg Blog

Go(Golang) Build, Mind Blown

Build go app really easy just:

shell code snippet start

go build -o named_out_put program.go

shell code snippet end

You can run the command without program.go if your go program is call main.go

And for more important, it support cross platform compile natively!

Like I do my development on my Macbook M2 which is arm64 platform. If we run above go build command, go default compile in my machine which is support arm64 only.

If we deploy the compiled program to x86 linux, it will not success to run the program. But! we can fix this just only add some arguments with build command:

For target to linux x86:

code snippet start

GOOS=linux GOARCH=amd64 go build -o named_out_put program.go

code snippet end

Then you can free to run the program on linux server without any go env and dependency!

It was shock me, it just easy to use and totally 0 configuration.Just one code file then can build to any platform like linux, windows, macos, even android!

Golang trap

variable reassign issue

golang code snippet start

func main() {
   count := 0
   if count < 5 {
   	count := 10
   	count++
   }
   fmt.Println(count == 11) // result is false
}

golang code snippet end

Here, the trap is the count at the end is 0…