Go Recipes

Table of Contents

Go Recipes

This is a collection of discussions about how to use Go to solve problems and perform tasks. The focus is on practical problems that make up small parts of larger problems. Hopefully you'll find the examples interesting and useful. You don't need to have read the preceding recipes to use them; they are supposed to be self contained but you might find more detail and explanation in surrounding recipes. You can also just browse for ideas on how to write Go code.

See the section below on using gosh to run the code for advice on how you can see immediately from the command line what the recipe code does.

Using gosh to run the code

The discussions are accompanied by fragments of Go code which can be run using the gosh command. See Gosh Tips for details.

go install github.com/nickwells/utilities/gosh@latest

To run the code simply paste it onto the command line for gosh, suitably quoted, after a -e parameter. Like this:

gosh -e 'fmt.Print("Hello,")
     fmt.Println(" World!")'

This will print "Hello, World!".

Alternatively you can use the -e-stdin parameter to gosh which will read the code from the standard input. This allows you to just repeat the previous command, paste in the code and terminate it with an EOF character (typically Control-D on Linux or Mac-OS)

See the section on gosh tips for lots more about how you can use gosh.

Evaluating solutions

Some of the solutions use both standard library packages and third-party packages. When deciding which solution to use be aware that using a third party library introduces an extra dependency into your program. If the standard library solution meets your needs you are generally better off using that. The Go team will maintain this code and you can be sure that it will still be around into the foreseeable future. Third party packages can offer some very powerful alternatives and different approaches to problems that may be just right for you.

Thanks

Thanks to fniessen (see github.com/fniessen/org-html-themes) for the themes used to make the html generated from the emacs orgmode source look as it does here.

Contact me

I am on Twitter @nickgwells and I'd be happy to hear from you. I've got some more recipe pages planned, if there are any you'd like to see please let me know.

Author: Nick Wells

Created: 2023-08-28 Mon 19:43

Validate