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

redirect output of an already running process

Long story short: you have launched your script/program but forgot to redirect the output to a file for later inspection. #!/usr/bin/python3 #sample endless running program that prints to stdout import time,datetime while True: print(datetime.datetime.now().time()) time.sleep(1) Using GNU Debugger you can re-attach to the process, then invoke the creation of a logfile and duplicate the file descriptor to make the system send the data to the new file, instead of the terminal: ...

April 24, 2015 · Andrea Manzini

Sincronizzare una directory tra due server linux

Obiettivo vogliamo mantenere la stessa directory sincronizzata su due server linux. Questo significa che ogni aggiunta/rimozione/modifica di file in questa directory verrà automaticamente riportato sull’altro (salvo conflitti). Diamo per assunto che i due server siano raggiungibili via rete, ma per qualsiasi motivo non sia possibile collegare dello spazio disco condiviso. Implementazione Per raggiungere lo scopo, utilizzeremo il tool: csync2 su entrambi i server (che chiameremo nodo1 e nodo2), installiamo i pacchetti necessari: ...

March 20, 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