sample template usage in the Go Programming Language

The GO programming language has a nice and useful standard library, which includes a powerful templating engine out of the box. Here I wrote an example, generating HTML output from a simple data structure. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package main import ( "html/template" "log" "os" ) func main() { page := ` <!DOCTYPE html> <html><head><title>my todo list</title></head> <body><h1>my TODO list</h1> <ul> {{ range $item := . }} <li> {{ $item.Priority }} {{ $item.Topic }} </li> {{ end }} </ul> </body></html> ` type Todo struct { Priority int Topic string } var todos = []Todo{ {1, "Take out the dog"}, {2, "Feed the cat"}, {3, "Learn GO programming"}, } t := template.Must(template.New("page").Parse(page)) err := t.Execute(os.Stdout, todos) if err != nil { log.Println("executing template:", err) } } This program generates the following HTML output: ...

September 30, 2015 · Andrea Manzini

A new project to learn the D Programming Language

I’ve started a new side project named D Lang Koans, it’s a simple series of exercises organized in a unit test suite. The “koans” are heavily inspired by similar projects for other languages, but I didn’t found anything similar for D. I’ll try to maintain and evolve it in the spare time, I hope you’ll find it useful.

December 7, 2014 · Andrea Manzini

semplice rate limit in Sinatra

Giocando con Sinatra ho avuto l’esigenza di servire una determinata pagina solo con un certa frequenza (tecnicamente un rate-limit); la cosa si puo’ fare installando il middleware Rack:Throttle ma non volevo aggiungere un’altra gemma alle dipendenze… In questo esempio se al server arriva piu’ di una richiesta in un intervallo di cinque secondi, rispondiamo a tono… SECONDS_BETWEEN_REQUEST=5 enable :sessions def ratelimit? now=Time.new.to_i session['lastrequest']||=0 #inizializza se non presente result=(now-session['lastrequest'])<SECONDS_BETWEEN_REQUEST #passati dall'ultima richiesta ? session['lastrequest']=now # aggiorna return result end get '/' do if ratelimit? return "<h1>sorry, rate limit exceeded!</h1>" end "<h1>hello!</h1>" end

December 21, 2012 · Andrea Manzini

background tasks in Ruby e linux

A volte negli script Ruby ho bisogno di controllare l’esecuzione di un comando eseguito in modalita’ asincrona, ho creato pertanto una classe apposita: class BackgroundJob def initialize(cmd) @pid = fork do # this code is run in the child process # you can do anything here, like changing current directory or reopening STDOUT exec cmd end end def stop! # kill it (other signals than TERM may be used, depending on the program you want # to kill. The signal KILL will always work but the process won't be allowed # to cleanup anything) Process.kill "TERM", @pid # you have to wait for its termination, otherwise it will become a zombie process # (or you can use Process.detach) Process.wait @pid end end come si usa ? Molto semplice: ...

November 5, 2012 · Andrea Manzini