Bacancy Bacancy
  • Our Engagement Models

    We offer flexible engagement models tailored to your project scope and timeline.

    Software Development Outsourcing
    Staff Augmentation
    Dedicated Teams
    Engagement Models

    High-Quality, Affordable IT Outsourcing

    Explore All Services

    AI & ML

    • AI Strategy & Roadmap
    • AI Development
    • Generative AI
    • AI Agent Development
    • Machine Learning
    • Computer Vision
    • LLM Development
    • RAG Development

    Data

    • Data Engineering
    • Data Analytics & BI
    • Data Science
    • Data Warehouse
    • Data Governance

    Cloud

    • Cloud Consulting
    • Cloud Migration
    • AWS Services
    • Azure Services
    • GCP Services
    • Kubernetes
    • DevOps

    Product Engineering

    • Full Stack Development
    • Custom Software Development
    • SaaS Development
    • App Modernization
    • Mobile App Development
    • Web Development
    • UI/UX Design

    Platforms

    • Salesforce
    • SAP
    • ServiceNow
    • Microsoft Dynamics
    • Snowflake
    • Databricks
  • Why Bacancy

    14+ years building domain-specific products for regulated and high-growth industries worldwide.

    Global delivery, every time zone

    Global delivery, every time zone

    1050+ vetted engineers

    1050+ vetted engineers

    Onboard in 48 hours

    Onboard in 48 hours

    Explore Our Case Studies

    Industries

    • Healthcare
    • Fintech
    • Real Estate
    • Logistics
    • Education
    • eCommerce
    How Bacancy Built a Custom Epic EHR Solution to Automate Clinical Workflows

    How Bacancy Built a Custom Epic EHR Solution to Automate Clinical Workflows

    Read case study
  • About.

    1050+ world-class tech experts. One standard: customer satisfaction.

    About Company

    About Company

    • About Us
    • Leadership Team
    • Customer Reviews
    • Infrastructure
    • Our Locations
    • Partnership

    Resources

    • Press Room
    • Blog
    • Insights
    ISO/IEC 27001:2022 Certified

    ISO/IEC 27001:2022 Certified

    Built for enterprise security and compliance.

    Get Quote
  • Technologies
  • Customer Stories
  • Contact Us
Find a Developer
book a 30 min call
  • AI Strategy & Roadmap
  • AI Development
  • Generative AI
  • AI Agent Development
  • Machine Learning
  • Computer Vision
  • LLM Development
  • RAG Development
  • Data Engineering
  • Data Analytics & BI
  • Data Science
  • Data Warehouse
  • Data Governance
  • Cloud Consulting
  • Cloud Migration
  • AWS Services
  • Azure Services
  • GCP Services
  • Kubernetes
  • DevOps
  • Full Stack Development
  • Custom Software Development
  • SaaS Development
  • App Modernization
  • Mobile App Development
  • Web Development
  • UI/UX Design
  • Salesforce
  • SAP
  • ServiceNow
  • Microsoft Dynamics
  • Snowflake
  • Databricks
  • Healthcare
  • Fintech
  • Real Estate
  • Logistics
  • Education
  • eCommerce
  • About Us
  • Leadership Team
  • Customer Reviews
  • Infrastructure
  • Our Locations
  • Partnership
  • Press Room
  • Blog
  • Insights

Technologies

Customer Stories

Contact Us

Find a Developer
book a 30 min call
GoRoutines

GoRoutines, Channels and Concurrency in a Nutshell (Part 1 of 4)

Ashvin Kumbhani
Ashvin Kumbhani Director of Engineering
Last Updated on June 3, 2024 | Written By: Ashvin Kumbhani

Hey Fellow Gophers,

After a long time, I have decided to jot something down, and this time I have taken up a bit of an ambitious topic. I am going to cover some of the significant features of Go in a series of four blogs “GoRoutines, Channels and Concurrency“. This is a first blog post, where I would like to get you through GoRoutines.

Mr. Bean

GoRoutines, let’s get into the details.

Do you agree that we have a habit of pursuing multiple things at the same time? Yes, we all do because such things make human beings unique. Such things are very common when we play the radio while doing household chores, singing songs while driving or listening to songs while programming.

GoRoutine allows us to do multiple at a time. It helps the CPU to multitask based on how we specify to it. As per the Golang website `A GoRoutine is a lightweight thread of execution` which enables us to be multithread.

The nature of Golang execution is synchronous, but GoRoutines enables us to run it asynchronously. Or to run a few functions concurrently.

Let’s understand this with a simple example. Assume that we have to wait 30 seconds before printing “Hello” and before printing “World”. First, it will code it using the traditional way that is without GoRoutines then I will do it with GoRoutines. We will monitor the time it takes individually.

Before that let’s have a quick look at the rules of GoRoutines

  • To run a function as a GoRoutine, we have to precede the function call with the keyword `Go`
    E.g. If we need to call the function RunAsynchronously() as a GoRoutine we will call it as `go RunAsynchronously()`
  • As soon as the synchronous function execution stops, the GoRoutine execution stops as well regardless of the completion of the function – I have mentioned in on a later stage.

First of all, let’s create a function that will calculate the time execution for the code programmed in it.

package main
 import (
        	"fmt"
        	"time"
)
 
func simple(funcToExec func()) {
        	start := time.Now()
        	funcToExec()
        	elapsed := time.Since(start)
        	fmt.Println("Time Taken for function to execute:  ", elapsed)
}
func main() {
}

So traditionally the code would look something like this.

package main
import (
        	"fmt"
        	"time"
)
func simple(funcToExec func()) {
        	start := time.Now()
        	funcToExec()
        	elapsed := time.Since(start)Ç
        	fmt.Println("Time Taken for function to execute:  ", elapsed)
}
func main() {
        	fmt.Println("Traditional Execution")
        	simple(PrintHelloWorldTraditionally)
}
func PrintWith30SecDelay(text string) {
        	time.Sleep(30 * time.Second)
        	fmt.Println(text)
}
func PrintHelloWorldTraditionally() {
        	PrintWith30SecDelay("Hello")
        	PrintWith30SecDelay("World")
}

Output:

Traditional Execution
Hello
World
Time Taken for function to execute:   1m0s

Now let’s see the difference if we run one of the print as a GoRoutine

package main
import (
        	"fmt"
        	"time"
)
func simple(funcToExec func()) {
        	start := time.Now()
        	funcToExec()
        	elapsed := time.Since(start)
        	fmt.Println("Time Taken for function to execute:  ", elapsed)
}
func main() {
        	fmt.Println("Traditional Execution")
        	simple(PrintHelloWorldTraditionally)
        	fmt.Println("Execution with GoRoutines")
        	simple(PrintHelloWorldWithGoRoutines)
}
func PrintWith30SecDelay(text string) {
        	time.Sleep(30 * time.Second)
        	fmt.Println(text)
}
func PrintHelloWorldTraditionally() {
        	PrintWith30SecDelay("Hello")
        	PrintWith30SecDelay("World")
}
func PrintHelloWorldWithGoRoutines() {
        	go PrintWith30SecDelay("Hello")
        	PrintWith30SecDelay("World")
}

Output:

Traditional Execution
Hello
World
Time Taken for the function to execute:   1m0s
Execution with GoRoutines
Hello
World
Time Taken for the function to execute:   30s

So the call of the function that prints “Hello” and of the function that prints “World” happens concurrently. Thus the delay is reduced.

But if you switch the GoRoutine declaration to be

PrintWith30SecDelay("Hello")
go PrintWith30SecDelay("World")

instead of

go PrintWith30SecDelay("Hello")
PrintWith30SecDelay("World")

It will result in not printing of the word “World”. But why so?

It is because the execution of `PrintWith30SecDelay(“Hello”)` happens a microsecond earlier to that of `PrintWith30SecDelay(“World”)`. Resulting into completion of `PrintWith30SecDelay(“Hello”)` to be a microsecond earlier.

So how can we solve this specific problem? The answer is with the help of WaitGroups.

Let’s look at the code on how we would do it?

package main
import (
        	"fmt"
        	"sync"
        	"time"
)
 
var wg sync.WaitGroup
func simple(funcToExec func()) {
        	start := time.Now()
        	funcToExec()
        	elapsed := time.Since(start)
        	fmt.Println("Time Taken for function to execute:  ", elapsed)
}
func main() {
        	fmt.Println("Traditional Execution")
        	simple(PrintHelloWorldTraditionally)
        	fmt.Println("Execution with GoRoutines")
        	simple(PrintHelloWorldWithGoRoutines)
}
func PrintWith30SecDelay(text string, wgAdded bool) {
        	if wgAdded {
                  	defer wg.Done()
        	}
         	time.Sleep(30 * time.Second)
        	fmt.Println(text)
}
func PrintHelloWorldTraditionally() {
        	PrintWith30SecDelay("Hello", false)
        	PrintWith30SecDelay("World", false)
}
func PrintHelloWorldWithGoRoutines() {
        	wg.Add(1)
        	go PrintWith30SecDelay("Hello", true)
        	wg.Add(1)
        	go PrintWith30SecDelay("World", true)
        	wg.Wait()
}

Output:

Traditional Execution
Hello
World
Time Taken for the function to execute:   8s
Execution with GoRoutines
Hello
World
Time Taken for the function to execute:   4s

Note: GoRoutines and WaitGroups are to be used carefully, or it is likely for it to end up not working correctly or being stuck in a Deadlock.

This is something that we can achieve with channels as well that I am going to cover it in the next blog.

In case, if you are looking for the Golang experts to build a world-class enterprise app with high speed, high security, and high modularity, then hire golang developer from us. Our Golang programmers have in-depth knowledge and extensive experience in converting potential ideas into a viable business solution.

Regarding this blog, do let me know your thoughts in the comments section below. I will soon be back with the next part `GoRoutines, Channels and Concurrency in a Nutshell: Part 2 (Channels)` coming soon.

Ciao.

Happy Coding Gophers!


Expand Your Digital Horizons With Us.

Start a new project or take an existing one to the next level. Get in touch to start small, scale-up, and go Agile.


Or
E-mail us : solutions@bacancy.com

Your Success Is Guaranteed !

Related Articles

Ashvin Kumbhani

June 5, 2026

Golang

Golang for Healthcare Application Development: Secure, Fast, HIPAA-Ready

By : Ashvin Kumbhani

Read More
Ashvin Kumbhani

April 22, 2026

Golang

Golang Cloud Native Development: Cost, Performance, Kubernetes & Scalability

By : Ashvin Kumbhani

Read More
Ashvin Kumbhani

February 18, 2026

Golang

How Much Does Golang App Development Cost in 2026?

By : Ashvin Kumbhani

Read More

Offices and Development Centers

Bacancy Ahmedabad Ahmedabad

15-16, Times Corporate Park, Thaltej, Ahmedabad, 380059

Bacancy Gandhinagar Gandhinagar

422-A, 4th Floor, Pragya Tower Road 11, Block 15, Zone 1, SEZ-PA Gandhinagar, 382355

Bacancy Hyderabad Hyderabad

Awfis, Level 1, N Heights, Plot No 38, Phase 2, Hitech City Hyderabad, 500081

Bacancy Mumbai Mumbai

18th Floor, Cyberone, opp. CIDCO Exhibition Centre, Sector 30, Vashi, Navi Mumbai, 400703

Bacancy Pune Pune

2nd FloorMarisoft-1, Marigold IT Park, Pune - 411014

Bacancy Bengaluru Bengaluru

Raheja Towers, 26/27, Mahatma Gandhi Rd, East Wing, Craig Park Layout, Ashok Nagar, Bengaluru, 560001

Global Presence

Bacancy New Jersey New Jersey

33 South Wood Ave, Suite 600, Iselin NJ 08830

Bacancy California California

535 Mission St 14th floor, San Francisco, CA 94105

Bacancy Massachusetts Massachusetts

501 Boylston St, Boston, MA 02116

Bacancy Florida Florida

4995 NW, 72nd Avenue, Suite 307, Miami, FL, 33166

Bacancy London London

90 York Wy, London N1 9AG, United Kingdom

Bacancy Ontario Ontario

71 Dawes Road, Brampton, On L6X 5N9, Toronto

Bacancy Australia Australia

351A Hampstead Rd, Northfield SA 5085

Bacancy UAE UAE

One Central 8th and 9th Floor - Trade Centre - Trade Centre 2 - Dubai - United Arab Emirates

Bacancy Sweden Sweden

Junkergatan 4, 126 53 Hagersten

Get in Touch

Great Place to Work

Get in Touch

cal-icon

Looking for expert advice?

Schedule a Expert Call


  • Brochure
  • Quality Assurance
  • Resources
  • Tutorials
  • Customer Reviews
  • Privacy Policy
  • FAQs
  • Press Room
  • Contact Us
  • Sitemap
  • Employee

bacancy google review 4.6
bacancy google review
bacancy clutch review 4.8
bacancy clutch review
bacancy glassdoor review 4.5
bacancy glassdoor review
bacancy goodfirms review 4.8
bacancy goodfirms review
iso
  • Bacancy Behance
  • Bacancy Pinterest

Copyright © 2026 BACANCY SERVICES PRIVATE LIMITED All rights reserved.