avatarRodney Daut

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3474

Abstract

w <code>SafeMap</code>.</li></ul><h1 id="f98e">Step 4: Set Method</h1><p id="a46c">Now, let’s create a method to set key-value pairs in our map. Add this code to the <code>SafeMap</code> struct:</p><div id="3d52"><pre><span class="hljs-comment">// Set sets a key-value pair in the map.</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(m *SafeMap)</span></span> Set(key <span class="hljs-type">string</span>, value <span class="hljs-type">int</span>) { m.mu.Lock() <span class="hljs-keyword">defer</span> m.mu.Unlock() m.data[key] = value }</pre></div><ul><li>We define a method called <code>Set</code> that takes a key and a value as parameters.</li><li><code>m.mu.Lock()</code> locks the mutex to ensure exclusive access to the map.</li><li>We use <code>defer</code> to ensure that the mutex is unlocked even if an error occurs or the function returns.</li><li>Finally, we set the key-value pair in the map.</li></ul><h1 id="a568">Step 5: Get Method</h1><p id="cefd">Let’s create a method to retrieve values based on keys. Add this code:</p><div id="2071"><pre><span class="hljs-comment">// Get retrieves the value associated with a key.</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(m *SafeMap)</span></span> Get(key <span class="hljs-type">string</span>) (<span class="hljs-type">int</span>, <span class="hljs-type">bool</span>) { m.mu.Lock() <span class="hljs-keyword">defer</span> m.mu.Unlock() val, ok := m.data[key] <span class="hljs-keyword">return</span> val, ok }</pre></div><ul><li>We define a method called <code>Get</code> that takes a key as a parameter and returns both the value and a boolean indicating if the key exists.</li><li>We lock the mutex, retrieve the value from the map, and unlock the mutex before returning the result.</li></ul><h1 id="9e28">Step 6: Delete Method</h1><p id="1411">To delete key-value pairs, add the following code:</p><div id="569d"><pre><span class="hljs-comment">// Delete removes a key-value pair from the map.</span> func (m *SafeMap) <span class="hljs-built_in">Delete</span>(key string) { m<span class="hljs-selector-class">.mu</span><span class="hljs-selector-class">.Lock</span>() defer m<span class="hljs-selector-class">.mu</span><span class="hljs-selector-class">.Unlock</span>() <span class="hljs-built_in">delete</span>(m.data, key) }</pre></div><ul><li>We define a method called <code>Delete</code> that removes a key-value pair based on the provided key.</li><li>Similar to other methods, we lock the mutex, perform the deletion, and unlock the mutex.</li></ul><h1 id="e8e5">Step 7: Size Method</h1><p id="13e9">Let’s create a method to determine the size of the map:</p><div id="f476"><pre><span class="hljs-comment">// Size returns the number of key-value pairs in the map.</span> func (m *SafeMap) <span class="hljs-built_in">Size</span>() int { m<span class="hljs-selector-class">.mu</span><span class="hljs-selector-class">.Lock</span>() defer m<span class="hljs-selector-class">.mu</span><span class="hljs-selector-class">.Unlock</span>() return <span class="hljs-built_in">len</span>(m.data) }</pre></div><ul><li>We define a method called <code>Size</code> that returns the number of key-value pairs in the map.</li><li>As with other methods, we lock the mutex, obtain the size, and unlock the mutex.</li></ul><h1 id="4996">Step 8: Putting It All Together</h1><p id="4eee">Now that we’v

Options

e defined our custom thread-safe map, let’s put it to use:</p><div id="327a"><pre><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { <span class="hljs-comment">// Create a new thread-safe map.</span> safeMap := NewSafeMap()

<span class="hljs-comment">// Set key-value pairs concurrently.</span> wg := sync.WaitGroup{} <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i < <span class="hljs-number">100</span>; i++ { wg.Add(<span class="hljs-number">1</span>) <span class="hljs-keyword">go</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(i <span class="hljs-type">int</span>)</span></span> { <span class="hljs-keyword">defer</span> wg.Done() key := fmt.Sprintf(<span class="hljs-string">"key%d"</span>, i) safeMap.Set(key, i) }(i) } wg.Wait()

<span class="hljs-comment">// Get and print values concurrently.</span> <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i < <span class="hljs-number">100</span>; i++ { wg.Add(<span class="hljs-number">1</span>) <span class="hljs-keyword">go</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(i <span class="hljs-type">int</span>)</span></span> { <span class="hljs-keyword">defer</span> wg.Done() key := fmt.Sprintf(<span class="hljs-string">"key%d"</span>, i) val, ok := safeMap.Get(key) <span class="hljs-keyword">if</span> ok { fmt.Printf(<span class="hljs-string">"Key: %s, Value: %d\n"</span>, key, val) } <span class="hljs-keyword">else</span> { fmt.Printf(<span class="hljs-string">"Key: %s not found\n"</span>, key) } }(i) } wg.Wait()

<span class="hljs-comment">// Delete keys concurrently.</span> <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i < <span class="hljs-number">50</span>; i++ { wg.Add(<span class="hljs-number">1</span>) <span class="hljs-keyword">go</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(i <span class="hljs-type">int</span>)</span></span> { <span class="hljs-keyword">defer</span> wg.Done() key := fmt.Sprintf(<span class="hljs-string">"key%d"</span>, i) safeMap.Delete(key) }(i) } wg.Wait()

<span class="hljs-comment">// Print the final size of the map.</span> size := safeMap.Size() fmt.Printf(<span class="hljs-string">"Map size: %d\n"</span>, size) }</pre></div><ul><li>In the <code>main</code> function, we create an instance of our <code>SafeMap</code>.</li><li>We set key-value pairs concurrently using goroutines.</li><li>We retrieve and print values concurrently.</li><li>We delete keys concurrently.</li><li>Finally, we print the size of the map.</li></ul><h1 id="d1df">Conclusion</h1><p id="c100">You’ve just created a custom thread-safe map in Go! This example demonstrates the basics of using mutexes to protect concurrent access to data structures. Understanding these concepts is crucial for building reliable concurrent programs in Go.</p><p id="2e1d">Feel free to explore my repository at <a href="https://github.com/parv-jain/thread-safe-data-structures">https://github.com/parv-jain/thread-safe-data-structures</a> where I plan to delve deeper into and experiment with the implementation of thread-safe data structures in Go.</p></article></body>

How To Profitably Pursue Your Passions With The Need-Connection Approach

Doing what you love and making it profitable at the same time

Photo by Ian Schneider on Unsplash

How did one man accidentally kill the oldest tree in history?

In 1964, Donal Rusk Currey got his tree corer stuck in a Great Basin Bristlecone Pine. A park ranger helped him get it out by cutting down the tree. Once the ranger felled the tree, Currey went to work counting the rings. He found the tree had been nearly 5,000 years old. The oldest living thing ever discovered.

Currey’s mistake was assuming the tree was much younger. Like most trees of that species, it was only 20 feet tall. And no one realized such small trees could live so long. Similarly, as entrepreneurs, we can make a mistake based on a common misconception — that we should create projects based solely on what the customer wants or needs.

Why is focusing purely on customer needs a mistake?

Because we get into business to do what we want. We want to pick our projects. We want to pursue our interests and passions. We don’t want to feel we have to do the work. We desire work we choose to do. But if you only create what clients or customers want without regard to your wishes, it’s a bit like having a job again — doing things just to fulfill someone else’s dream.

But don’t we have to give customers what they want?

Yes. But you satisfy your interests and curiosity first. We first find work we want to do, so our business is fulfilling for us. Then we forge a connection between the work we love to do and our customer’s needs. This way, you and the customer are both happy.

We do that with the need-connection method.

What is the need-connection method?

It’s a way to discover a need your project can fulfill. It assumes that the need to solve problems is a powerful motivator. If your car breaks down, you call a mechanic. If your house is infested with termites, you call an exterminator. If your tooth hurts, you call a dentist. If you show your clients that you can fix the toothache in their lives or businesses, they’ll give you the freedom to do the work you love.

How do you find your client’s toothache?

First, identify the thing you want to do. Second, figure out who you want to help with what you do. Third, find out from the client what problems they have. Fourth, figure out how your project will solve one of those problems.

Here are some applications of these four steps

Author Sean D’Souza wrote a book called The Secret Life of Testimonials. If you check Google Ads or Amazon, you’ll find there isn’t much demand for such a book. Yet, he sells quite a few copies every year. How?

He’d already accomplished the first step of writing the book, and he knew he wanted to help the people he already served, which were small business owners. So he needed to connect this book to one of their problems.

These business owners often complained about getting “yucky” clients. Instead, they wanted clients who were a joy to work with. His approach to testimonials solves that problem. So that’s how he sold the book — As a way to get testimonials that attract the clients you love.

One client, a belief coach who lives in the UK, needed to grow his business. He loved helping people to change their beliefs, which allowed them to change their feelings and behavior. To market his services, I had to help him find a group he wanted to serve. He was once an aspiring actor, and so he decided to help actors. What problem did many of them have that he could help with? Many suffered from anxiety, which kept them from performing at their best during auditions.

Another coach from Australia had a system to help people improve their mindset. In my experience, such a general service is hard to market. In our brief conversation, I discovered she’d written a book. Are you interested in helping would-be-authors? She wasn’t sure. I asked if she used her mindset tools to overcome difficulties in writing the book. She said yes.

Then I suggested she could help others like herself who want to write a book but haven’t overcome the challenges of doing so. If she decides to pursue would-be-authors, she’ll be doing the mindset work she loves but also fulfilling a need. We all know many people who say they have a book in them but never seem to get it written.

As you can see, the need-connection method allows you to pursue your passion in a way that is profitable to you and beneficial to your clients

So decide what you want to do, figure out who you want to help, and connect your work to a problem they want solved. You’ll have success in getting clients or customers without feeling like you’ve given yourself a job you don’t love.

Want more marketing goodness? Click here to join my weekly newsletter and get a free report on three marketing mistakes you can easily avoid and what to do instead.

Entrepreneurship
Marketing
Business
Startup
Ideas
Recommended from ReadMedium