Golang: Web Deployment Experience


I've finished building a web based application with golang. The last step is deploy that app to the production server. In this post I want to share how to deploy golang code, especially a web based application.

There are at least 3 ways to deploy golang code. Firstly, deploy source code and run it directly. This is the safest way to deploy because if there is any unsatisfied dependency, go run will complain. You can use watcher tool like gin with this way, it will watch on code changes and do live reload. 

Second, deploy the source code and run binary resulted by go build. This way is not really different than first way, but you cannot use any available watcher tool. Because you run the binary file. The third is deploy only binary file. Wait, what if the target architecture is different? Golang has multi os and architecture build parameter which are GOOS and GOARCH. You can specify them with your target requirement.

In my opinion, deploying only a binary file is the most efficient way. Golang build tool go build was smart enough to build a binary file with no dependency. Except, you, by accident, need C binding and hard to make it statically linked like in my circumstance. I used combination between second and third way. I deployed source code to staging server then build a binary file. I deployed that binary file to production server. In my circumstance, I develop in OS X and using library that bind C with CGO. Both my staging and production server are Centos. GOOS and GOARCH are not friendly with CGO. I choose to build in staging server and deploy that binary to production server.

*image from l-lin.github.io