loading...
Cover image for See Glitch Go!

See Glitch Go!

shindakun profile image Steve Layton Originally published at shindakun.glitch.me on ・4 min read

A friend of mine recently presented an interesting case for his learning Go. He happens to use a lot of Containers in his day to day work and since Kubernetes, Docker, and most of the HashiCorp programs are written in it then he figured it may be worth knowing. During the conversation, I lamented that I missed the opportunity to really learn Go - to which he correctly responded, "It's never too late!".

After all, IT IS never too late to learn something new and while I do use containers from time to time I don't use them quite that much - still it'd be good to have some basic skills with Go regardless, after all, Go isn't just for containers. With that in mind, we're going to revisit a small Go application and Glitch site that I put together.

Bear in mind that Glitch currently does not "support" Go however, instances do have Go version 1.7.5 installed.

We'll start by creating a new Glitch app and deleting all the files but package.json.

See Glitch Go!

This will effectively "break" the current app so you will see "Error" on the logs button, that's OK we'll fix that shortly. For now, we'll focus on what we want Go to do. Since Glitch works so well for website hosting it makes sense that we set up a very basic web server.

Click on New File and enter server.go then click Add File 👍. A typical first programming example would be to create a 'hello world' application so let's do that. Here is our complete code, which we'll go through in steps afterward.

package main

import (  
  "fmt"
  "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {  
    fmt.Fprintf(w, "Hello Go!")
}

func main() {  
    http.HandleFunc("/", handler)
    http.ListenAndServe(":3000", nil)
}

Every Go program starts with what is known as a Package Declaration. Packages help you organize and reuse code, we are going with main because this is will be "main" executable for this program and it isn't being shared as a library - it also seems to be the standard for Go programs.

package main

To create our Go web server will need to import a couple standard libraries that already exist. fmt is one of Go's input/output libraries, it gives us methods to actually present information to the user. A typical "hello world" program would simply print to the screen, in our case we want to print to a web browser instead. To do that we'll import net/http. This gives us the tools we need to create a server that listens on a TCP port and the ability to receive and respond via HTTP. The net/http package page linked here has several different examples of very simple servers.

import (  
  "fmt"
  "net/http"
)

Next, we declare a function called handler which we can later pass to the http library. This function will take in the HTTP request as well as the HTTP response writer so we can respond to the browser. Here we are printing the string "Hello Go!" to the http.ResponseWriter which we've named 'w' in this example.

func handler(w http.ResponseWriter, r *http.Request) {  
    fmt.Fprintf(w, "Hello Go!")
}

Every Go program contains a main function. This is the function that is called when the program is executed. For this example, upon startup, we will register our handler function by calling http.HandleFunc and passing it the path we want to respond on and the name of the function to handle that request to. Using / this way means every path on our server will respond with "Hello Go!", nothing should 404.

With our handler registered, we can create the actual server itself. http.ListenAndServe will start up the HTTP server listening on port 3000. Glitch has a layer in between our server and the public internet which will translate this into port 80 (or 443) so we don't need to worry about routing that part.

func main() {  
    http.HandleFunc("/", handler)
    http.ListenAndServe(":3000", nil)
}

So far so good! Here is the tricky part. Unlike node.js, Go is a compiled language so we'll need to account for that as it can't simply start by calling node and your startup script. Glitch does give us the tools we need though as it uses the package.json file to start the node site, we're simply going to change it to compile our node application first.

To compile a Go binary from the command line you would call build as shown below, we use server.go since that's the name of the new file we created at the start.

go build server.go

That would build our executable but we need to run it too. The start scripts section of package.json functions more or less like the command line does. So, we can update the script to

"scripts": {
    "start": "go build server.go && ./server"
  },

This will compile our app and then run it. You can see my complete package file in the image below or on Glitch itself.

See Glitch Go!

One final note on this project - as it is it will not rebuild when you make changes to the server.go file. You would need to make a change to the package.json file or wait for the server to time out and restart. For the extra curious I have an extended example with a few different routes and some parsing over at https://glitch.com/edit/#!/go-example. It also includes a watch.json file to set up auto rebuilding.



Posted on Nov 18 '17 by:

shindakun profile

Steve Layton

@shindakun

I've been known to write some code from time to time.

Discussion

markdown guide