Golang Utilities

I am now working through “For The Love Of Go”.
This is a more modern introduction to Go that the Go Programming Language book.

It teaches the language starting with tests.

Chapter 2 introduces gofmt which can be used to find formatting errors.
Oddly they can be fixed with go fmt

Insights From The Unicorn Project

I am now reading The Unicorn Project.
This book is an equal to the The Phoenix Project, that is it happens at the same time as the Phoenix Project with overlapping characters but with a different focus.

The Five Ideals

  1. Locality and Simplicity
  2. Focus Flow and Joy
  3. Improvement of Daily Work
  4. Psychological Safety
  5. Customer Focus

Four Types of work

  1. Planned work
  2. Internal Projects
  3. Changes and Updates
  4. Unplanned work

Notes on Oath

Last year I bought the game Oath: Chronicles of Empire and Exile.

It’s a complicated (and interesting) game for 1-6 players. The starting position of each game is determined by the territories owned by the winner of the previous game.

The board is divided into three regions. The Cradle, Provinces and the Hinterlands. It’s cheaper to move within a region. The sites are cards that are dealt within a region. These will shift between games.

Sites can have items to be collected, may have relics to be recovered and can have special effects (for example you need to discard a card, of you have one, to move out of The Slums)

This is a Euro game (although can be a long one). The game will end at the completion of the 8th round, although the Empire gets a chance to roll a die to finish early from the 5th round. They can only do this if the Empire owns the Oathkeeper marker. Exiles can win earlier if they manage to hold the Userper token at the start of their turn or satisfy a Vision at the start of their turn.

Each player has a character card that holds the warbands that travel with their pawn, a supply track and has space for three advisors (some of these may be face down). It’s a three card hand limit!

There are multiple Victory conditions and they are all based upon actions in the game – there are no abstract victory points. The Chancellor (who rules the Empire) is trying to achieve one of the four Oaths that were determined by the winner of the previous game.

The options are:

– Control more sites than anyone else

– Own the Banner of the People’s Favour

– Own the Banner of the Darkest Secret

– Own more relics and banners than anyone else.

The Exile players need to either achieve that victory condition and hold it at the start of two consecutive turns or they can find a Vision card that allows a different victory condition to be applied.

The Chancellor can also make an offer of Citizenship to an Exile. Citizens can win of the empire wins but they satisfy the secondary victory condition.

In each player’s turn they get to spend Supply on a number of actions. The more warbands you control the less supply is available (as you need to pay your warbands).

Actions that cost supply include:

– Searching for new cards (Either from the World Deck or your current regions discard pile). You take three cards and discard the two (or three) that you don’t want to play. When searching from the World Deck the search stops when a vision is drawn. The cost of drawing from the World Deck increases with each Vision card found.

– Moving around the board (the cost varies)

– Mustering warbands (from a denizen at your current location).

– Reclaiming Relics or Banners

– Trading Favours or Secrets (Give away one get more of other)

– Campaigning, which is to attack a site or another player.

There are also actions that don’t require supply:

– Playing a face down advisor (either to your card or to the site you are at.)

– Use a power of a denizen or advisor that you control or are at the site of.

If you need to discard a card it will go to the next region along the board (which wraps around).

A site can only have warbands belonging to a single player at a time (or it belongs to the local warbands). Of you own a site you can take advantage of all of the denizens that reside there.

The Banners initially seem to be only useful for Victory so we mostly ignored them in the first game. They do have useful features.

The Banner of The People’s Favour assists in the placement of Denizens. You can remove a denizen from the region that you are in and place them at another site within the same region. This can help offset the increasing Favour cost (you need to feed it favour at the start of your turn).

The Banner of the Darkest Secret keeps the search cost down (which normally increases as the number of visions has been revealed).

The Chancellor can also exile Citizens but it costs Favour.

Denizens each have one of the six associated suits. The suits each have a banks of Favour. When trading Favour from a denizen you can only take from the Bank. When you place a Denizen you can one Favour from the general supply. The Favour banks form an interesting economic model – you need to have as many denizens of a given suit to boost the payout from trading Secrets/Favours. The downside is that this can quickly exhaust the banks.

Secrets put on a Denizen will come back to you at the end of your turn. Favours go the matching bank.

The game is mostly about the balance of power and trying to stop whoever is about to win. The deck of Denizens gets biased towards the majority of the advisors that belong to the winner. This can mean that over time some suits will become more common.

There are about 200 Denizens, each with unique abilities (and beautiful artwork). It can be hard to work out which to keep and which to discard.

Exercism.org is great

exercism.org is a great place to learn languages.

For various work related reasons I am working through the Go and the Java tracks. I am also working through the Elixir track.

The main catch I am having is language overlap.

Go and Elixir don’t end lines with semicolons, Java does.

Go uses lowercase for module names and PascalCase for (public) functions.
Elixir uses PascalCase for module names and snake_case for functions.

It does not help that I have written more Groovy than Java and that also ignores semicolons.

However the concept trees are a great way to understand language specifics:


Time Travel with a Message Queue

I was discussing the usages of message queues with some colleagues and surprised them with temporal coupling.

Message Queues perform buffers between applications so that they can process data at different rates. It also protects from outages allowing each side to be restarted or upgraded independently.

However most modern message queues have additional options such as the ability to send a message to the future! The message is sent hidden for a time period. The trick to to send a command that will rollback the operation you are just starting. If your process stops normally then this message can be deleted before it is read. If the process dies then the message will appear and can be used to clean up. This is like having a finally block that will survive a process restart!

Another trick is to send messages that are deleted after a given time. I have used this to have a log available that starts logging 5 minutes ago. This means that when a problem occurs you can capture the last 5 mins of events leading up to it. This can prove invaluable.

Golang is weird!

I have just managed to get Golang to use local packages.

Go takes the approach that all packages are to be considered to be remote even if they are not.
This means that you need to define your projects package location in go.mod as if it were installed from the remote repository that you will deploy it to. You can then import the local module as if it were from that location but it will resolve to a local copy.

package github.com/company-name/project-name/project-root

You can then import github.com/company-name/project-name/project-root/mymodule

from a ./mymodule folder in your project. All go files in that folder will be imported.

The practical advantage is that you can use the same sample code if the library is local or remote without needing a distinct bundling tool.

The go test command is also odd. It by default only runs the tests in the current folder.
You need to use go test. ./... to get all the tests to run (which will be all files named *_test.go in the filesystem.

This is not very intuitive.

Integrating Typescript and Frontend Javascript

Recently I have been working on a AngularJS/Angular hybrid application.
Part of this involve having parallel routers. This requires coordinating the two systems as to which routes should be Angular and which should be AngularJS

Typescript does not allow build time metaprogramming that can generate the required javascript.

The typescript compiler cannot generate browser js.

Angular CLI does not allow the creation of modules that are not used for dynamic loading.

I considered using xslt to create both files that we needed.

In the end I used a json file that is checked by the typescript tests and put in the assets area so deployed by the Angular CLI.

Golang Magical Reference Date

Go solves the date parsing by using a magical reference date.

You start with this date

Mon Jan 2 15:04:05 MST 2006

Adjust it into whatever format that you use and use it for parsing dates.
This is neat solution. It would be even better if the go documentation for time.Parse were to mention it.

I found the solution here: https://programming.guide/go/format-parse-string-time-date-example.html

This has now been raised as https://github.com/golang/go/issues/48757

Thoughts on Golang

I am currently studying Golang as I may need to use it in an upcoming project.

So far it is C-- `as it is C with certain dangerous features removed.

There is no macro system. You can’t do pointer arithmetic.

The compiler is not quite as helpful as some languages that I have used recently (Elm and Elixir).

It’s not object-oriented or functional. You get old school procedural programming.

Given that it is compiled there is no repl to help. The documentation is a bit restricted. For example when looking at date parsing there are no examples on building custom strings.

It is fast and handles complex numbers natively.

Not yet reached using goroutines.

Thoughts on Security

It is almost impossible to secure a web app. You cannot tell if a browser is being used or a script simulating it. Anything that can be called from the browser should be treated as a public API (with the exception of insisting on backwards compatibility).

This means any endpoint reachable from the client must be treated as if the user is directly calling it. This means that you need to consider authentication (who is the user) and authorisation (what the user is allowed to do). Trust no-one!

This means that all endpoints need to be tested at the API level as well as via the UI.

All those bundled minified js bundles that you have carefully constructed form excellent documentation of your exposed APIs. All the endpoints exist as neat little strings among the minified code.