Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

Date:

Share post:

So, I was messing around with Go the other day, and I had this kinda silly idea: What if I made two Goroutines “fight” each other? Like, not really fight, but, you know, compete in some way. I called it “gorilla fights gorilla” because, well, Goroutines… gorillas… get it? Anyway, here’s how it went down.

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

The Setup

First, I needed a basic Go environment. I already had Go installed, so I just created a new directory and a new Go file called . Nothing fancy.

The “Fight”

I needed something for the Goroutines to compete over. I decided on a simple counter. One Goroutine would try to increment the counter, and the other would try to decrement it. A classic tug-of-war!

Here is the function that I create for running each Goroutine:


func run(name string, increment bool, counter int, done chan bool) {

for i := 0; i < 1000; i++ {

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

if increment {

counter++

*(name, " Increment, now counter is",counter)

} else {

counter--

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

*(name, " Decrement, now counter is",counter)

done <- true

Then, I start to write main function:


func main() {

counter := 0

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

done := make(chan bool)

go run("Gorilla 1", true, &counter, done)

go run("Gorilla 2", false, &counter, done)

<-done

<-done

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

*("Final counter:", counter)

I spun up two Goroutines. Gorilla 1 was the incrementer, and Gorilla 2 was the decrementer. They both had access to the shared counter variable. I need to make sure only one Goroutine messes with the counter at a time, so each Goroutine add and minus by one.

After launching the Goroutines, I used the done channel to wait for both of them to finish. This is important, without this, the program would just exit before the Goroutines even got a chance to run!

The Results

I ran the code, and … it worked, and it would print who is doing what now! Each time I run it, I get a different result for who “wins”. Because, this is decided by the scheduler. Sometimes Gorilla 1 gets ahead, sometimes Gorilla 2 does. It’s all down to the whims of the Go scheduler.

Wrapping Up

So, that was my silly little “gorilla fights gorilla” experiment. It wasn’t groundbreaking, but it was a fun way to play around with Goroutines and see how they interact with shared resources. And it reinforced the importance of always remembering that concurrent programming can be unpredictable!

Gorilla Fights Gorilla: Find The Reason Behind The Conflict!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related articles

Who is Isaiah Hartenstein? Get the Latest Updates Here!

Okay, so today I wanted to dig into something totally new for me – “Isaiah Hartenstein”. I’d heard...

Leah Belfort: Learn All About Her.(fast and simple facts)

Okay, so I’ve been seeing this “Leah Belfort” thing popping up all over my socials, and I was...

Understanding 1.e-28: A Beginners Guide to this Number

So, the other day I was messing around with some really tiny numbers in my code, and I...

Raiders Mock Draft 2024: Who Will Las Vegas Pick? (Our Latest Predictions)

Okay, so I’ve been diving deep into mock drafts lately, because, well, who isn’t? It’s that time of...