Tag Archives: Golang

The Barber Shop Problem in Go

A similar solution was posted here. I wanted to look at this problem because it is a classic example of handling interprocess communication.

In this problem one models a stream of customers coming into a Barber Shop. If the waiting room, or lobby, is full, then they leave, otherwise they take a seat and wait for a haircut. The barber(s) take customers, cut their hair, and then look for the next customer.

In this code one channel is filled randomly with Customers; some of these will wait in the lobby, others will look for another barber. Within the BarberShop function, there is a barbers slice, and a channel named syncBarberChan. When the function runs, barbers are initialized and added to the slice. When a customer is selected and paired with a barber, the barber is taken out of the slice. At this point the haircut occurs for some period of time. When the haircut is over, the barber is added to the syncBarberChan. Next the syncBarberChan is drained for any idle barbers. If a customer is waiting, that newest barber is paired with a customer, otherwise the barber is added back to the slice of barbers.

In this way, the slice holds idle barbers, and the channel is used to signal that a barber is done with a given task.

Continue reading

Traversing a Directory Tree in Python and Go(lang)

In this post I’ll discuss the basics of walking through a directory tree in Python and Go. If you are dealing with a smaller directory, it may be more convenient to use Python. If you are dealing with a larger directory containing hundreds of subdirectories and thousands of files, you may want to look into using Go, or another compiled language. I enjoy using Go because it compiles quickly, and it doesn’t use pointer arithmetic.

Continue reading