In my series of “Follow me learning Golang” I’ve already shared some code snippets with you. But you may ask yourself already how I code, no?
Currently I use Atom without any Golang specific modifications expect the language-go package. I don’t remember wether this comes preinstalled with Atom or I installed it 😅. Anyway, this is installed. But not more.
To run my code I’ve build a very basic Dockerfile
which will execute go run hello.go
at the end.
FROM golang:1.11.5-alpine3.8
WORKDIR /go/src/app
COPY hello.go .
CMD [“go”, “run”, “hello.go”]
As you see my hello.go
file is on my machine and sits together with the Dockerfile
in a directory.
I used the following command to build and run it:
docker build -t go . && docker run go
There are directly two questions which raise up here:
- Why haven’t you installed go on your machine?
- Why do you build the image first all the time and then execute it?
The answer to the first question is pretty simple. I don’t know 😅. I haven’t really thought about it. As I started with writing my first lines of code the Docker solution was which came in my mind first. On the other hand — why not? I mean what would be the benefit of having go installed on your machine? Of course you wouldn’t have the Docker “overhead”. But on the other hand this solution will always work. On all computers. You only have to install Docker. Whatever. Currently I don’t see any reasons or benefits of not using this solution and install go on my machine directly.
I asked the second question to myself as well. And I came to the conclusion that this is pretty bad (to build the image again and again each time I change in my hello.go file). So I decided to update my Dockerfile
to something like “you can find my hello.go
file here. Please run that.”. With that solution I could build my image only once and have to run it when I’ve updated my code.
But because I am very interested of having an executable which I can run I improved my Dockerfle
even more.
My final solution “use this directory containing the source code and build a executable for my MacBook running a x86_64 macOS” looks like this:
FROM golang:1.11.5-alpine3.8WORKDIR /go/src/app
VOLUME WORKDIR# Build the App for macOS x86_64
ENV GOOS=darwin
ENV GOARCH=amd64CMD [“go”, “build”]
Now I only have to build it one time and can run it with:
docker run -v ${PWD}:/go/src/app [NAME_OF_THE_IMAGE]
The generated executable is then available in ${PWD}
named app
.
Anyway, I don’t want to dig too deep into the Docker world. So I don’t explain that further. But what I want to explain are the environment variables here because they are Golang related.
In fact you can build your Golang project for different operating systems and architectures. But what I don’t know if you can build on any type of machine to any other “format”. But I know that I can build on a Linux host (Alpine Linux) for my MacBook (macOS x86_64) by only settings the environment variables GOOS
and GOARCH
. The complete list of available options can be found here.
That means if I want to build the same thing for Android (hell yeah — even this is possible!) I could simply change the variables to GOOS=linux
(because Android is basically a Linux derivate) and GOARCH=arm
and et voila:
Learning sources
I’ve learned that I have to use go run [file].go
to run my go file from here. I just googled around for the “how to get a Golang executable” and found this post and the already mentioned Golang environment page.
The Docker stuff is already in my head. I haven’t learned something new here.