This week in our Discovery series we will talk about the Go programming language. This language, created in 2007, has an important distinctive feature compared to all the other languages we have already talked about and which are supported by alwaysdata (PHP, Python, Ruby, Node.js and Lua)…
A compiled language
Go is a compiled language, which therefore does not need an interpreter to run programs. This has a major consequence: Go does not even have to be installed at alwaysdata to be used. The 1.7 version is nevertheless available on our servers so that you can compile your programs in SSH, but you can just as well compile your programs elsewhere — on your own computer, for example.
Example: deploying a Macaron application
There are of course many web frameworks for Go. As we are fond of food (and French), we will use the Macaron framework in the following example.
To illustrate the fact that development and compilation can be done locally, we will run the commands on our own computer — here under Linux, but it could be under MacOS, Windows, or under any other environment:
- Let’s download Macaron:
1 | $ go get gopkg.in/macaron.v1 |
- Let’s create the file of our application, hello.go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package main import ( "os" "strconv" "gopkg.in/macaron.v1" ) func main() { m := macaron.Classic() m.Get("/", func() string { return "Hello world!" }) host := os.Getenv("IP") port, _ := strconv.Atoi(os.Getenv("PORT")) m.Run(host, port) } |
- Let’s compile the program:
1 | $ go build hello.go |
- We now have an executable file named hello. We will send it by SCP to the root of our alwaysdata account (named superman in this example):
1 | $ scp hello superman@ssh-superman.alwaysdata.net:~/ |
- Last step, let’s create in the alwaysdata admin a User Program type site, by indicating:
- Command: ~/hello
That’s it: our Go program will start automatically by going to the specified address!
Next language to discover on our blog: Elixir.
Hey folks! please upgrade to 1.9, there is no reason to stay on 1.7 , 2 versions ago that long, thanks!
Est-il possible d’upgrader le compileur à la version 1.9 ? 1.7 date de plus d’un an, cela évite au gens d’avoir à compiler leur programmes non compatible avec 1.7 sur leur machines.
Actually there’s a good reason: the latest version available in Debian (which we use) is 1.7. Since it’s pretty easy to use your own compiler and we don’t have a lot of users developing in Go yet, creating our own package of Go to have the latest version is not worth the trouble. But if Go becomes more popular on alwaysdata, that may change!
Hi alwaysdata, when will the Elixir post be ready? I have a small application I would love to get out there.
Hopefully by the end of next week!