Title photo
frugal technology, simple living and guerrilla large-appliance repair

Regular blog here, 'microblog' there

Many of my traditional blog post live on this site, but a great majority of my social-style posts can be found on my much-busier microbloging site at updates.passthejoe.net. It's busier because my BlogPoster "microblogging" script generates short, Twitter-style posts from the Linux or Windows (or anywhere you can run Ruby with too many Gems) command line, uploads them to the web server and send them out on my Twitter and Mastodon feeds.

I used to post to this blog via scripts and Unix/Linux utilities (curl and Unison) that helped me mirror the files locally and on the server. Since this site recently moved hosts, none of that is set up. I'm just using SFTP and SSH to write posts and manage the site.

Disqus comments are not live just yet because I'm not sure about what I'm going to do for the domain on this site. I'll probably restore the old domain at first just to have some continuity, but for now I like using the "free" domain from this site's new host, NearlyFreeSpeech.net.

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.