Tag: Concurrency
Wrapping Goroutines to Handle Return Values in Go
Introduction
Concurrency is one of Go’s superpowers, enabling developers to run tasks in parallel using goroutines. A goroutine is launched with the go keyword followed by a function call, as shown below:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Go playground link to the example
This simple example prints “hello” and “world” concurrently. However, the Go language specification notes that if a function called in a goroutine returns values, those values are discarded. This raises a question: how can we capture and use return values from a goroutine?
Embracing 'Share By Communicating' in Go
Introduction
Concurrency is a core strength of Go, and one of its standout principles is “Share By Communicating.” Unlike traditional approaches that rely on shared memory and locks, Go encourages developers to share data between goroutines by passing it through channels. This method simplifies concurrent programming and helps avoid common issues like race conditions, making your code safer and easier to maintain.
Why does this matter? In many languages, concurrent programs share memory directly, requiring locks to prevent conflicts. This can lead to complex, error-prone code, think deadlocks or subtle bugs that only appear under specific conditions. Go flips this on its head: instead of locking memory, you communicate data through channels, ensuring only one goroutine accesses it at a time by design.
Tag: Error-Handling
Wrapping Goroutines to Handle Return Values in Go
Introduction
Concurrency is one of Go’s superpowers, enabling developers to run tasks in parallel using goroutines. A goroutine is launched with the go keyword followed by a function call, as shown below:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Go playground link to the example
This simple example prints “hello” and “world” concurrently. However, the Go language specification notes that if a function called in a goroutine returns values, those values are discarded. This raises a question: how can we capture and use return values from a goroutine?
Tag: Golang
Wrapping Goroutines to Handle Return Values in Go
Introduction
Concurrency is one of Go’s superpowers, enabling developers to run tasks in parallel using goroutines. A goroutine is launched with the go keyword followed by a function call, as shown below:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Go playground link to the example
This simple example prints “hello” and “world” concurrently. However, the Go language specification notes that if a function called in a goroutine returns values, those values are discarded. This raises a question: how can we capture and use return values from a goroutine?
Embracing 'Share By Communicating' in Go
Introduction
Concurrency is a core strength of Go, and one of its standout principles is “Share By Communicating.” Unlike traditional approaches that rely on shared memory and locks, Go encourages developers to share data between goroutines by passing it through channels. This method simplifies concurrent programming and helps avoid common issues like race conditions, making your code safer and easier to maintain.
Why does this matter? In many languages, concurrent programs share memory directly, requiring locks to prevent conflicts. This can lead to complex, error-prone code, think deadlocks or subtle bugs that only appear under specific conditions. Go flips this on its head: instead of locking memory, you communicate data through channels, ensuring only one goroutine accesses it at a time by design.
My Readings
Learnings
This is a page where I will add my readings and learning resources for tracking and sharing with others:
Non Fiction
Drive: The Surprising Truth About What Motivates Us
Golang
- Wild Golang Interface appeared
Reference: https://go.dev/doc/effective_go#pointers_vs_values Sample Code: https://go.dev/play/p/oLIg7Wpc6w2
Fiction
Experience with Go workspace
Introduction
Go 1.18 added the workspace mode to Go, allowing you to work on multiple modules simultaneously without having to edit the go.mod file.
Module is a folder having go.mod
file
Earlier if you are working with multiple modules the flow would look like below
- Make the change in module1.
- Update the go.mod file of module2 with
replace
directive for your local, unpublished changes. - Make the changes in module2.
- Before pushing to remote remember to remove the
replace
from thego.mod
.
What workspace allows you to do is allow you to access the changes from module1 in module2 without updating the go.mod
files.
Thus allowing developers to fully focus on writing code, only when you are going to complete your local development flow and are going to push to remote, we need to update the version tag of module1 in module2’s go.mod file.
Tag: Goroutines
Wrapping Goroutines to Handle Return Values in Go
Introduction
Concurrency is one of Go’s superpowers, enabling developers to run tasks in parallel using goroutines. A goroutine is launched with the go keyword followed by a function call, as shown below:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Go playground link to the example
This simple example prints “hello” and “world” concurrently. However, the Go language specification notes that if a function called in a goroutine returns values, those values are discarded. This raises a question: how can we capture and use return values from a goroutine?
Tag: Synchronization
Wrapping Goroutines to Handle Return Values in Go
Introduction
Concurrency is one of Go’s superpowers, enabling developers to run tasks in parallel using goroutines. A goroutine is launched with the go keyword followed by a function call, as shown below:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Go playground link to the example
This simple example prints “hello” and “world” concurrently. However, the Go language specification notes that if a function called in a goroutine returns values, those values are discarded. This raises a question: how can we capture and use return values from a goroutine?
Tag: Channel
Embracing 'Share By Communicating' in Go
Introduction
Concurrency is a core strength of Go, and one of its standout principles is “Share By Communicating.” Unlike traditional approaches that rely on shared memory and locks, Go encourages developers to share data between goroutines by passing it through channels. This method simplifies concurrent programming and helps avoid common issues like race conditions, making your code safer and easier to maintain.
Why does this matter? In many languages, concurrent programs share memory directly, requiring locks to prevent conflicts. This can lead to complex, error-prone code, think deadlocks or subtle bugs that only appear under specific conditions. Go flips this on its head: instead of locking memory, you communicate data through channels, ensuring only one goroutine accesses it at a time by design.
Tag: Interface
My Readings
Learnings
This is a page where I will add my readings and learning resources for tracking and sharing with others:
Non Fiction
Drive: The Surprising Truth About What Motivates Us
Golang
- Wild Golang Interface appeared
Reference: https://go.dev/doc/effective_go#pointers_vs_values Sample Code: https://go.dev/play/p/oLIg7Wpc6w2
Fiction
Tag: Config
Experience with Go workspace
Introduction
Go 1.18 added the workspace mode to Go, allowing you to work on multiple modules simultaneously without having to edit the go.mod file.
Module is a folder having go.mod
file
Earlier if you are working with multiple modules the flow would look like below
- Make the change in module1.
- Update the go.mod file of module2 with
replace
directive for your local, unpublished changes. - Make the changes in module2.
- Before pushing to remote remember to remove the
replace
from thego.mod
.
What workspace allows you to do is allow you to access the changes from module1 in module2 without updating the go.mod
files.
Thus allowing developers to fully focus on writing code, only when you are going to complete your local development flow and are going to push to remote, we need to update the version tag of module1 in module2’s go.mod file.
Tag: Productivity
Experience with Go workspace
Introduction
Go 1.18 added the workspace mode to Go, allowing you to work on multiple modules simultaneously without having to edit the go.mod file.
Module is a folder having go.mod
file
Earlier if you are working with multiple modules the flow would look like below
- Make the change in module1.
- Update the go.mod file of module2 with
replace
directive for your local, unpublished changes. - Make the changes in module2.
- Before pushing to remote remember to remove the
replace
from thego.mod
.
What workspace allows you to do is allow you to access the changes from module1 in module2 without updating the go.mod
files.
Thus allowing developers to fully focus on writing code, only when you are going to complete your local development flow and are going to push to remote, we need to update the version tag of module1 in module2’s go.mod file.
Tag: Array
Group Anagrams
Problem Statement
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: strs = [“eat”,“tea”,“tan”,“ate”,“nat”,“bat”]
Output: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = [“a”]
Tag: Data Structure
Group Anagrams
Problem Statement
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: strs = [“eat”,“tea”,“tan”,“ate”,“nat”,“bat”]
Output: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = [“a”]
Two sum
Problem Statement
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Valid Anagram
Problem Statement
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “anagram”, t = “nagaram”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Contains Duplicate
Problem Statement
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Example 1:
Input: nums =
\[1,2,3,1\]Output: true
Example 2:
Input: nums =
\[1,2,3,4\]Output: false
Example 3:
Input: nums =
\[1,1,1,3,3,4,3,2,4,2\]Output: true
Solutions:
1. Brute force
Given that we have an array of elements to identify if a given element is duplicated, we will compare the element with the rest of the array and this needs to be done for all the elements in the array.
Introduction To Data Structures
In this post we are going to look at
- What are Data Structures and Abstract Data Types
- Why do we need them
- How can we create our own simple ADT
Let’s get started
What are Data Structures and Abstract Data Types and Why do we need them
In any program there are lots of operations being performed ranging from simple addition to complex ones. These operations have a very basic thing in common regardless of their scale.
Tag: Easy
Two sum
Problem Statement
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Valid Anagram
Problem Statement
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “anagram”, t = “nagaram”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Tag: Hash Map
Group Anagrams
Problem Statement
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: strs = [“eat”,“tea”,“tan”,“ate”,“nat”,“bat”]
Output: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = [“a”]
Two sum
Problem Statement
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Valid Anagram
Problem Statement
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “anagram”, t = “nagaram”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Contains Duplicate
Problem Statement
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Example 1:
Input: nums =
\[1,2,3,1\]Output: true
Example 2:
Input: nums =
\[1,2,3,4\]Output: false
Example 3:
Input: nums =
\[1,1,1,3,3,4,3,2,4,2\]Output: true
Solutions:
1. Brute force
Given that we have an array of elements to identify if a given element is duplicated, we will compare the element with the rest of the array and this needs to be done for all the elements in the array.
Tag: Introduction
Group Anagrams
Problem Statement
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: strs = [“eat”,“tea”,“tan”,“ate”,“nat”,“bat”]
Output: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = [“a”]
Two sum
Problem Statement
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Valid Anagram
Problem Statement
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “anagram”, t = “nagaram”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Contains Duplicate
Problem Statement
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Example 1:
Input: nums =
\[1,2,3,1\]Output: true
Example 2:
Input: nums =
\[1,2,3,4\]Output: false
Example 3:
Input: nums =
\[1,1,1,3,3,4,3,2,4,2\]Output: true
Solutions:
1. Brute force
Given that we have an array of elements to identify if a given element is duplicated, we will compare the element with the rest of the array and this needs to be done for all the elements in the array.
Introduction To Data Structures
In this post we are going to look at
- What are Data Structures and Abstract Data Types
- Why do we need them
- How can we create our own simple ADT
Let’s get started
What are Data Structures and Abstract Data Types and Why do we need them
In any program there are lots of operations being performed ranging from simple addition to complex ones. These operations have a very basic thing in common regardless of their scale.
Number Systems
Number Systems
Here we are going to take a look at the following Number systems which are used extensively in computer science
- Decimal
- Binary
- Octal
- Hexadecimal
Introduction
Basically, all the systems are used for counting, thus allowing humans to measure things.
Every system that we are going to see will have a radix/base.
Radix / Base - the number of unique digits, including the digit zero, used to represent numbers [1]
Tag: Medium
Group Anagrams
Problem Statement
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: strs = [“eat”,“tea”,“tan”,“ate”,“nat”,“bat”]
Output: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = [“a”]
Tag: Basic
Contains Duplicate
Problem Statement
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Example 1:
Input: nums =
\[1,2,3,1\]Output: true
Example 2:
Input: nums =
\[1,2,3,4\]Output: false
Example 3:
Input: nums =
\[1,1,1,3,3,4,3,2,4,2\]Output: true
Solutions:
1. Brute force
Given that we have an array of elements to identify if a given element is duplicated, we will compare the element with the rest of the array and this needs to be done for all the elements in the array.
Introduction To Data Structures
In this post we are going to look at
- What are Data Structures and Abstract Data Types
- Why do we need them
- How can we create our own simple ADT
Let’s get started
What are Data Structures and Abstract Data Types and Why do we need them
In any program there are lots of operations being performed ranging from simple addition to complex ones. These operations have a very basic thing in common regardless of their scale.
Number Systems
Number Systems
Here we are going to take a look at the following Number systems which are used extensively in computer science
- Decimal
- Binary
- Octal
- Hexadecimal
Introduction
Basically, all the systems are used for counting, thus allowing humans to measure things.
Every system that we are going to see will have a radix/base.
Radix / Base - the number of unique digits, including the digit zero, used to represent numbers [1]
Tag: Documentation
The documentation conundrum
Introduction
In this world of information as a software engineer we have some great resources that guide us and provide us with information required for our daily activities.
Some of the references available to us are:
- stackoverflow.com
- medium.com
- Engineering blogs written by an organization or by an individual
- Github Repositories
- Recently some LLMs like ChatGPT, Bard etc.
- Youtube/Tutorials/MOOC
- Private Groups.
- And many more
Now there is one specific resource that I missed out that is the Official Documentation for the piece of software in use. This post is all about my observations in the recent years.
Tag: Engineering
The documentation conundrum
Introduction
In this world of information as a software engineer we have some great resources that guide us and provide us with information required for our daily activities.
Some of the references available to us are:
- stackoverflow.com
- medium.com
- Engineering blogs written by an organization or by an individual
- Github Repositories
- Recently some LLMs like ChatGPT, Bard etc.
- Youtube/Tutorials/MOOC
- Private Groups.
- And many more
Now there is one specific resource that I missed out that is the Official Documentation for the piece of software in use. This post is all about my observations in the recent years.
Using Truth Table in Development.
Introduction
Truth tables are the foundation of boolean algebra and are required for logical reasoning and software implementation.
All the if
else
statements we write, irrespective of the programming languages, use
boolean algebra.
One such scenario became the inspiration for this particular short post
The Initial Details
To display some orders on a page where users have access to multiple filters,
such as
- search box - to search the order directly using some identifier
- date range filter - to get orders with a specific date range
- status - to get orders with a specific status
and many others
Tag: Reading
The documentation conundrum
Introduction
In this world of information as a software engineer we have some great resources that guide us and provide us with information required for our daily activities.
Some of the references available to us are:
- stackoverflow.com
- medium.com
- Engineering blogs written by an organization or by an individual
- Github Repositories
- Recently some LLMs like ChatGPT, Bard etc.
- Youtube/Tutorials/MOOC
- Private Groups.
- And many more
Now there is one specific resource that I missed out that is the Official Documentation for the piece of software in use. This post is all about my observations in the recent years.
Tag: Experience
Using Truth Table in Development.
Introduction
Truth tables are the foundation of boolean algebra and are required for logical reasoning and software implementation.
All the if
else
statements we write, irrespective of the programming languages, use
boolean algebra.
One such scenario became the inspiration for this particular short post
The Initial Details
To display some orders on a page where users have access to multiple filters,
such as
- search box - to search the order directly using some identifier
- date range filter - to get orders with a specific date range
- status - to get orders with a specific status
and many others
Tag: Database
JSON datatype in postgreSQL
Introduction
PostgreSQL provides us with a datatype to store JSON data. This datatype can be helpful in lots of situations giving us a way to store some dynamic data.
Further PostgreSQL provides us with two types for storing JSON data
- json
- jsonb
They both take almost identical set of inputs but the major difference is that in the efficiency.
The json
data type stores the exact copy of the input text which the processing functions must reparse
on each execution.
On the other hand jsonb
data is stored in the decompressed binary format which makes it slightly slower
to input due to the added overhead of conversion, but significantly faster to process since no reparsing is needed.
Using Constants in PostgreSQL queries
Introduction
Imagine we have a long query which has a lots of conditions, and many of the conditions has some kind of comparisions with literals instead of some other record value.
Example:
SELECT * FROM users u where (u.name='some name' or u.age=12 or u.email='qwe@qwe.qwe');
For example sake we are taking the name, age and email as constants.
Imagine these literal values used multiple times in the query. We will have very untidy query.
Tag: Postgres
JSON datatype in postgreSQL
Introduction
PostgreSQL provides us with a datatype to store JSON data. This datatype can be helpful in lots of situations giving us a way to store some dynamic data.
Further PostgreSQL provides us with two types for storing JSON data
- json
- jsonb
They both take almost identical set of inputs but the major difference is that in the efficiency.
The json
data type stores the exact copy of the input text which the processing functions must reparse
on each execution.
On the other hand jsonb
data is stored in the decompressed binary format which makes it slightly slower
to input due to the added overhead of conversion, but significantly faster to process since no reparsing is needed.
Using Constants in PostgreSQL queries
Introduction
Imagine we have a long query which has a lots of conditions, and many of the conditions has some kind of comparisions with literals instead of some other record value.
Example:
SELECT * FROM users u where (u.name='some name' or u.age=12 or u.email='qwe@qwe.qwe');
For example sake we are taking the name, age and email as constants.
Imagine these literal values used multiple times in the query. We will have very untidy query.
Tag: Numbers
Number Systems
Number Systems
Here we are going to take a look at the following Number systems which are used extensively in computer science
- Decimal
- Binary
- Octal
- Hexadecimal
Introduction
Basically, all the systems are used for counting, thus allowing humans to measure things.
Every system that we are going to see will have a radix/base.
Radix / Base - the number of unique digits, including the digit zero, used to represent numbers [1]