• Using context.Context with cobra

    Even if creating CLI applications is straighforward with Go, there are many libraries to help with that task, specially to handle subcommands and flags. Cobra is likely one of the more popular ones. It’s relatively simple to use and it’s easy to get something working very quickly. A very basic example looks something like this: rootCmd := &cobra.Command{ Use: "demo", Short: "demo shows how to pass a context.Context to a cobra command", Run: rootFn, } if err := rootCmd.
  • go.mod's go directive

    Recently I ran into a curious behavior related to the go directive in the go.mod file. It’s well documented, but it’s nevertheless surprising. Go 1.11 introduced support for modules, as a way to define a collection of related Go packages. You can find details about its use and operation in the documentation. This support has continued to evolve throughout 1.12 and 1.13. A module is defined using a go.mod file, described here.
  • The Faneuil Hall problem

    The “Faneuil Hall” problem comes next in line in “The Little Book of Semaphores” and deals with taking the Oath of Citizenship in the city of Boston: There are three kinds of threads: immigrants, spectators, and a one judge. Immigrants must wait in line, check in, and then sit down. At some point, the judge enters the building. When the judge is in the building, no one may enter, and the immigrants may not leave.
  • The senate bus problem

    The next problem in “The Little Book of Semaphores” is called “the senate bus problem” and reads as follows: Riders come to a bus stop and wait for a bus. When the bus arrives, all the waiting riders invoke boardBus, but anyone who arrives while the bus is boarding has to wait for the next bus. The capacity of the bus is 50 people; if there are more than 50 people waiting, some will have to wait for the next bus.
  • The room party problem

    The author of “The Little Book of Semaphores” wrote this week’s problem inspired by real life events at Colby College. The problem’s statement is the following: The following synchronization constraints apply to students and the Dean of Students: Any number of students can be in a room at the same time. The Dean of Students can only enter a room if there are no students in the room (to conduct a search) or if there are more than 50 students in the room (to break up the party).
  • The child care problem

    Next problem in line in the the little Go book of semaphores series is about caring for children. It comes from the book “Operating Systems and Middleware: Supporting Controlled Interaction” by Max Hailperin (problem 4.6). It reads as follows: State licensing rules require a child-care center to have no more than three infants present for each adult To put it in a different way: there’s a room where both adults and children are found.
  • The sushi bar problem

    The next chapter in the the little Go book of semaphores series introduces the “not remotely classical problems” and the first one is called “the sushi bar problem”. Imagine a sushi bar with 5 seats. If you arrive while there is an empty seat, you can take a seat immediately. But if you arrive when all 5 seats are full, that means that all of them are dining together, and you will have to wait for the entire party to leave before you sit down.
  • The Modus Hall problem

    After that short intermission in reactive programming, it’s back to the little Go book of semaphores series with “the Modus Hall problem”. I didn’t understand the reference in the footnote and after some googling, it turns out that “Modus Hall” is the nickname given to a set of temporary student residence buildings at the Olin College of Engineering where the author of the problem, Nathan Karst, was a student. The problem reads as follows:
  • A basic reactive system

    I’m making a pause in the “semaphores” series to visit a problem from exercism.io, namely react in Go. As the description says, the idea is to implement a basic reactive system in Go. Wikipedia defines “reactive programming” as “a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow.
  • The unisex bathroom problem

    The next problem in the “The Little Book of Semaphores” is called the “unisex bathroom problem”, by Allen Downey himself. The problem statement reads as follows: A friend of the author took a job at Xerox. She was working in a cubicle in the basement of a concrete monolith, and the nearest women’s bathroom was two floors up. She proposed to the Uberboss that they convert the men’s bathroom on her floor to a unisex bathroom, sort of like on Ally McBeal.