In this article I want to share some concepts related to named and anonymous types in Go with an example exercise.

The exercise

Let’s take a text that represents a drawing with rectangles inside and our objective is to count them. The input is a []string input and the rectangles are drawn using 3 characters:

  • Vertices are drawn with the ‘+’character
  • Horizontal edges are drawn with the ‘-’ character
  • Vertical edges are drawn with the ‘|’ character

Here are a few examples:

A simple input with one rectangle

A simple input with 1 rectangle

A more complex input with 5 rectangles

A more complex input with 5 rectangles

Our objective is, given any of these inputs, to count all the rectangles.

#go #golang #anonymous

Named and Anonymous objects in Golang
3.20 GEEK