When working on a source code that has multiple contributors, it can be so that the feature branch that you are currently working on is behind by a few commits. This can happen when someone else on the team merges their commits to the main branch while you are working on the feature branch. To bring in the latest commits to your feature branch, you will need to either rebase to the main branch or merge the local main branch to your feature branch after pulling in the latest commits from the remote main branch.
Searching for an item in a collection or container is a common operation in programming. In Go, there are a few different ways that this can be done. In this snippet, let us explore the ways in which an item, be it a number or string, can be searched in a slice.
In today’s snippet, let’s look at how to use string builder and bytes buffer to concatenate a list of strings. You can always use the + operator to concatenate strings, but it is memory inefficient just like in so many other languages. Each time + is used to concatenate a string, it allocates a new string in memory. This is because, like in most languages, strings in Go is immutable. To avoid this inefficiency, we can use Go’s strings.Builder and bytes.Buffer type. Let’s look at how we can use these types to concatenate strings.
In today’s snippet, let’s look at how to generate checksum of a string or a file in Go. A checksum is a cryptographic signature of a file or a string. It is usually used to check if a file or stream of bits has been tampered or modified. If the file is modified, the checksum for the file changes. There are various cryptographic algorithms available to generate the checksum of a file or string, the popular algorithms are md5 and sha256. Let’s look at how we can generate these checksums in Go.
In today’s snippet, let’s look at how we can use synchronization techniques in Go to print natural numbers in order from two different goroutines. In our example here, we have two goroutines; one goroutine will print even numbers while the other goroutine will print odd numbers, but the output has to be in increasing order. To implement this, we will use a channel to synchronize the printing in the two goroutines. We will also use sync.WaitGroup to wait for the goroutines to finish before exiting the code. Note that we will need an unbuffered channel so that one goroutine is blocked while the other is printing the number. The even goroutine will use the done variable to indicate to the odd goroutine that it is done with the printing and there is nothing listening to the syncChannel. Without this, you will see a deadlock at the end of the printing.
A queue is a First-In-First-Out linear data structure, i.e., the element that is first inserted into the queue is the element that comes out first. The operation that involves inserting an element into the queue is called enqueue. The operation that involves removing an element from the queue is called dequeue. Normally, a queue is implemented using an array and two pointers; front and rear. This makes the time complexity of enqueue and dequeue operations O(1).
In this snippet, we will instead look at implementing a queue using a stack. This may increase the time complexity of the enqueue or dequeue operations, but the idea here is to see how such an implementation will look like. A stack is a Last-In-First-Out linear data structure. Here the last element inserted into the stack is the first element that comes out of the stack. It has push and pop operations where push inserts the element into the stack and pop removes the last inserted element from the stack.
In Go, we can use context to send cancellation signals to goroutines that is doing some work. The Done method in context returns a channel that acts as a cancellation signal to goroutines using the given context. When the channel is closed, it indicates to the goroutines that they should stop the work they are doing. We can use WithCancel context function to obtain a new context along with the cancel function that can be used to close the channel.
In order to test the snippet, we will use localstack to run s3 emulator in a docker container on the laptop. The container exposes s3 endpoint on port 4566. The snippet will then create a new bucket work-with-s3 in s3. The snippet will then delete all files from blog directory under work-with-s3 bucket.
In order to test the snippet, we will use localstack to run s3 emulator in a docker container on the laptop. The container exposes s3 endpoint on port 4566. The snippet will then create a new bucket work-with-s3 in s3. The snippet will then list all files in blog directory under work-with-s3 bucket.
In order to test the snippet, we will use localstack to run s3 emulator in a docker container on the laptop. The container exposes s3 endpoint on port 4566. The snippet will then create a new bucket work-with-s3 in s3. The snippet will then upload couple of files to blog directory under work-with-s3 bucket.
About
Bharghava Varun Ayada is a Staff Software Engineer at Walmart Labs, living in Bangalore, India. This blog is about software engineering, system design, distributed systems and DevOps.