Ajax with Golang

Lately, I thought of trying my hands on the classic Ajax alongside Go language. The requirement is to write a simple web server which sends ‘current timestamp’ to the requester every second. I have written a small program to do exactly the same. The Ajax part is taken care by jquery.

In order to see it in action, one needs to run it “go run tick-tock.go”. When the program is running, it essentially becomes a tiny web server listening to requests at port ‘9999’.

Now all you need to do is fire up a browser and raise a request to the tick-tock server: http://localhost:9999/time

Go is awesome. Isn’t it?

5 thoughts on “Ajax with Golang”

  1. Another tip:

    time.Now().Format(…) already returns a string, the extra cast is unnecessary.

    If you just want to write a string to a Writer without extra formatting, you can simply use fmt.Fprint instead of fmt.Fprintf:

    fmt.Fprint(w, page)
    fmt.Fprint(w, time.Now().Format(“Mon, 02 Jan 2006 15:04:05 MST”))

Leave a Reply

Your email address will not be published. Required fields are marked *