Go fragments

a collection a Go examples programs

About the construction of this site: a Go exploration

This site presents annotated examples of Go source code, that I found interesting or helpful when learning Go and then using it. I remember it was not too obvious at the beginning to find such a collection, even if many Go resources are available on the net (see Useful Links) here). At the beginning, I had found the simple GoByExample extremely helpful, as a collection (this site has been inspired by that work by M.McGrana). So, why not publish and share the snippets, fragments and pieces I progressively accumulated about Go. This is it! Hope it will be of help to someone.

Implementation

Obviously, the site is implemented in Go (with the necessary Javascript/html/css).

The static part of the site, and its functions (a simple “search” based on a reversed index on both title and content of each fragment, syntax highlighting, links, tags, categories validation, rss feeds, layouts/templating, markdown posts transformed into html documents (using the blackfriday package by Russ Ross), posts slugs generation, structure of the site, etc..) is generated from a single Go program. I will put it on Github, once cleansed a little bit.

The original Go source code are transformed into Markdown texts (the posts of this site), using in particular the Go Standard Library packages: regexp, text/template and the suite go/ast/format/parser/token.

And last, the site is implemented as an Appengine applications (using the excellent package “Goon” by MJibson/XStrom/MZimmerman and Co.).

That has been one one way for me to explore the Go environment and get acquainted with its remarkable community.

Hope some of these examples will be of help to someone. Hope the site will be easy and pleasant to browse.

Any suggestion is welcome.

Why Go (my point of view + those of many others go developers)

Go is easy (language design):

1 Simple language and simple syntax (hope the syntax highlighting I set up is convenient)

1.1  Go spec: 50 Pages
1.2  C++ spec: 1200 Pages

2 Garbage collected (no manual memory management)
3 Stack traces on nil dereference (not “segmentation fault”)
4 Pointers but automatically on stack wherever possible
5 Composition of types, not inheritance
6 Consistent design
7 Concurrency (goroutine, channels, select, the sync package provides synchronization primitives)
8 Large standard library
9 Go tools set:

9.1 Dowloading packages (go get)  
9.2 No makefiles (go install)   
9.3 Testing suite (go test)  
9.4 Race/Deadlock detection (go install -race)   
9.5 Automatic documentation (godoc)    
9.6 Code formatting (gofmt)    
9.7 Automatic import detection (goimports)   
9.8 Profiling (go tool pprof)   

10 Go is scalable:

 10.1 Language Design in the Service of Software Engineering
 10.2 Built for large programs and large teams
 10.3 Composition (types, functions, packages)

11 Go is fast:

 11.1 Goal within 10% of C
 11.2 Short compile times
 11.3 Easier parallel computing

When I review an ‘old/legacy’ Go program, it takes very little time to figure it out. Maintenance is easy since reading/understanding Go code is easy.

Avertissement

I’m a native french speaker, and got to writing in English late. The majority of the posts were originally written for a french version of the blog and then translated into english. If any errors in english (or Go), I would appreciate any feedback, comments, corrections, or advices.

Regarding the programming fragments you will find here, I have gathered them along my Go code reading, and I have not always mentioned the original author. Sorry for that, but if you recognize some of your code, just let me know and I will add the missing information (since I have decided to publish these snippets, I will keep the author mentioned as much as I can… but it is a recent ‘effort’, never planned initially!).

I believe they can be a complementary source of practical Go code examples such as ‘gobyexamples’, ‘sourcegraph’ , the golang standard library and others…). I am myself going over these examples often to retrieve useful validated implementations.

I hope this set will be useful to someone and that your feed-back will help improve the quality of the code presented and extend its scope. I will try to keep on adding anything of interest in the future.

This being said in english, une réflexion en Français pour finir (I am French).

“Dans le futur, les historiens (s’il en reste) reviendront sur le curieux spectacle qui prend forme au début du 21ème siècle. Pour la première fois dans leur histoire, les êtres humains sont confrontés à la prospective vraisemblable d’une catastrophe considérable, résultat de leurs comportements – comportements qui détruisent nos espoirs de survie décente.

Ces historiens observeront que le pays le plus riche et le plus puissant de l’histoire, jouissant d’avantages incomparables, conduit l’effort d’intensification de ce probable désastre. A la tête de l’effort pour protéger les conditions dans lesquelles nos descendants directs pourraient envisager une vie convenable se trouvent les sociétés prétendument “primitives”: les Indiens du Canada, les tribus, les indigènes, les arborigènes.”

Avram Noam Chomsky, March 2013 (traduit par P.Toca).