Friday 3 January 2014

Creating a simple web server in Golang

No modern programming language would be complete without the ability to interact over the internet. Go has this in spades, and since there's nothing more satisfying than getting a web page up and running, we are going to code up a simple web server in Go:
package main

import ("fmt"
        "net/http")

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello Web")
        })

    http.ListenAndServe(":8080", nil)
}
Save this code as "server.go" and then run it by opening a command prompt and typing:
go run server.go
You command prompt will pause. Open a browser and enter the location as:
http://localhost:8080
Your browser should return a page that says "Hello Web". Congratulations, you've just written your first web server in Golang.

Importing multiple packages

One of the new things we've done in our simple server is to import a second package. The net/http package allows us to work with the http protocol and listen for requests and serve responses. Importing multiple packages is pretty simple. There are various ways to do so, but the syntax above works fine - include your packages, one per line and surround the entire block of imports with parentheses.

Passing functions as arguments

The net/http package allows us to capture requests and route them with the HandleFunc - this function takes two parameters - the first, the url to handle and the second, a function which will be called when we hit the server on the specified url. This takes advantage of Go's first class functions which we explored a little before.

Listening"

The http.ListenAndServe function tells go that to "listen" on a particular port - we've specified ":8080", hence our url becomes "http://localhost:8080". Unlike our previous snippets of code, which exited once they had finished executing, our web server will run until we specifically interrupt it (or it hits a nasty error). To exit our program, pull up the command prompt where you typed "go run server.go" and hit Ctrl-C.

Thursday 2 January 2014

First class functions or passing arguments to functions in Golang - refactor

Last time, we learned how to pass arguments to functions in Go. We even managed to pass a function as an argument illustrating Go's First-Class Functions. We ended up with a small piece of code as follows:
package main
 
import "fmt"
 
func goodbye(aFunc func(words string) string) {
    fmt.Println(aFunc("Goodbye"))
}
 
func hello(aFunc func(words string) string) {
    fmt.Println(aFunc("Hello"))
}

func main() {
    var x = func(words string) string {
        return words + " world"
    }
    hello(x)
    goodbye(x)
 
}
We have two functions that take as an argument, another function. You will notice that both func goodbye and func hello have the same signature for their single argument. This kind of duplication is crying out for a refactor, and that's exactly what we are going to do. Instead of declaring the function signature as an argument (aFunc func(words string) string), we are going to create a type with that function signature. To declare a type in Go, we simply use the type keyword. Our function that will be used as an argument can be declared like this:
type myOwnFunction func(words string) string
We can then use our newly defined type. For example, our goodbye function can be re-written to use our new type:
func goodbye(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Goodbye"))
}
Similarly, our hello function:
func hello(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Hello"))
}
The rest of our code can remain untouched. A complete listing of our refactored code now looks like this:
package main
 
import "fmt"

type myOwnFunction func(words string) string
 
func goodbye(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Goodbye"))
}
 
func hello(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Hello"))
}
 
func main() {
    var x = func(words string) string {
        return words + " world"
    }
    hello(x)
    goodbye(x)
 
}