Title photo
frugal technology, simple living and guerrilla large-appliance repair
Sun, 05 Jul 2015

Learning Go: create a web server in five lines

The documentation for Go (aka Golang) is peppered with examples, and one of those examples, for Go's net/http package, shows you how to easily create a file server.

net/http is part of Go's standard library Here is the example code from golang.org/pkg/net/http:

package main

import (
    "log"
    "net/http"
)

func main() {
    // Simple static webserver:
    log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}

When you drop this code into a file in a directory (in my case I made the directory web_server and named the file main.go), then either compile it with go build or run it with go run, it creates a web server on port 8080 that serves the contents of your /usr/share/doc directory, which always exists in Linux and Unix (and probably in the Mac OS X version of Unix).

To see the results, open a web browser and go to http://localhost:8080/, and you should see a directory listing. Just like any web page, you can click on the links and see what's in those files.

This example program -- a web server in five lines -- is fun to play around with. You can change the http.Dir and serve "real" web content. You can change the port from :8080 to something else.

Sun, 21 Jun 2015

I wrote my Ode Indexette time-stamp program in golang

Last year I decided to write a short script that outputs the time/date-stamp line required for Ode's Indexette add-in.

Back in 2014, I did it in Perl, Ode's "mother" language. It's really just a two-liner with a whole lot of notes:

(Due to a quirk of Ode formatting and the $ character, I'm rendering this program via a Github Gist)

I've been playing around with lots of other languages since then. I know I should stick with one and really learn it, but for now it is what it is.

I decided to try to get the same output from the Google-created go (aka golang) programming language, and with the help of this web page, I was able to hack it together pretty quickly:

package main

 import (
    "fmt"
    "time"
 )


 func main() {

    // get the current time in UTC

     indexette_time := time.Now().UTC()

    /* print the time to standard output in the format
    required by Ode's Indexette add-in. Note that the 
    .Format parameters use an "old" date just to set 
    the format, the output will be the current time
    due to the use of time.Now() */

     fmt.Println("tag : Indexette : index-date :", indexette_time.Format("2006 01 02 15:04:05"))

}

I'm still calling the script into gedit the same way (through Snippets), and it works just as well as the Perl version.

One thing I just learned about go that's pretty cool is you can run your go program as a script, or compile it as a binary and run that. Advantages of a binary are that it's portable -- anybody with a system for which the binary is built can run it without needing to install go on their own system. And the binary should run faster than the script, though this is admittedly not an issue for three lines of code.

But it's cool anyway.

In the case of this script, I named it ode_time. Through experimentation, I figured out that the go build program that makes the binaries takes their name from the directory containing the file. So since I wanted the go binary to have the same name as the file, I gave the directory the same name, too:

My script file is here (I'm leaving out most of the path, but suffice it to say this is the place where I keep my program files):

/golang_code/ode_time/ode_time.go

I run the uncompiled script this way while in the /ode_time directory:

$ go run ode_time.go

I get this output:

Perfect!

I wanted to make a binary just because.

Here's how I did it. I am working in the /ode_time directory that contains ode_time.go:

$ go build ode_time.go

Now the directory contains two files:

ode_time ode_time.go

The first is the binary (which was automatically made executable by the go build command), and the second is the "raw" go script.

So I can now run the binary from my console like I'd run any binary that isn't in my path:

$ ./ode_time

And I get the same output.

The takeaway: I wanted to write a go program, and with the help of the Internet (and people who actually know how to do these things), I did it. And it was a program that I use on a daily basis -- whenever I write a blog post for my Ode system.

I like the idea of go, which is the language used by the Hugo static blogging system. The documentation seemed OK, but I did have to go "off the reservation" to find an example that I could work off of.

I'll clearly have to seek out tutorials and books if I want to pursue programming with go. Fortunately there are a few go books about to be released, and that might help me figure it out.