Working with Images in Go

The Image interface is at the core of image manipulation in Go. No matter what format you want to import or export from, it ultimately ends up as an Image. This is where the beauty of Go interfaces really shines. Go comes with support for gif, jpeg, and png formats in the standard packages. These examples demonstrate how to programatically generate, encode, decode, write to file, and base64 encode images. We will also cover a little bit about interfaces.

2D Arrays and Slices in Go

Two dimensional arrays and slices are useful for many situations. When creating an array in the Go programming language it is automatically initialized to the empty state, which is usually a 0. Arrays cannot be expanded. Arrays live in the stack and take up space in the compiled executable. Arrays are also passed by copy whereas slices pass pointers. Slices require a little more initialization at first but are more versatile. You can grab subsets of the arrays if needed, access elements directly, or expand the slices. Slices are thin wrappers around arrays and hold pointers to the array. They are initialized during run time and live in the heap.

HTML Templates in Go

Here is a quick example of how to use Go's html/template package to loop through a slice of structs and print out the contents. Note that in the templates {{.}} prints all the template variables, and . refers to the data passed.

type Person struct {
  var Name string
  var Age int

Simple struct definition for our Person

Why I Like Go

I think Go is a well designed and fun language to use. The standard library is pretty amazing, but here are a few things I like about Go. At the end are several references and links if you want to learn more about Go! Also check out my code snippets and references on GitHub github.com/NanoDano/reference