How Golang makes concurrent programming easier

How Golang makes concurrent programming easier

In this article we will explore how Golang makes the use of concurrent threads more easy.

Goroutines

Goroutine is a function that runs concurrently with other goroutines inside the same program. They are ligth and eficient allowing that you execute countless goroutines.

Different of traditional threads Goroutines are managed by Go internally, make the use of them more easy for the developer, providing a better developer experience in use.

How use?

Now that we know what goroutines are, let’s see a real-world example of how to use them.

go createGoRoutine()        

To create a goroutine, we simply add the go keyword before the function call, and Go will handle the rest automatically.

We can also use anonymous functions with goroutines. Let’s look at an example:

This example is from an application where we listen to WebSocket messages from Binance and check if a cryptocurrency is close to a target buy price.

go func() {
    for {
        _, message, err := conn.ReadMessage()
        if err != nil {
            log.Printf("Error reading message: %v", err)
            return
        }
        var trade TradeMessage
        if err := json.Unmarshal(message, &trade); err != nil {
            log.Printf("Error processing message: %v", err)
            continue
        }
        log.Printf("New trade: %s - Price: %s", trade.Symbol, trade.Price)
    }
}()        

📌 What happens here?

  • The goroutine allows the WebSocket listener to run in the background without blocking the main program.
  • Each new message from Binance is processed concurrently, improving performance.

Goroutines make concurrent programming simpler and more efficient. In the next section, we can explore channels, which allow secure communication between goroutines.

Gabriel Demétrio Gauche

Full Stack Software Engineer | Front-end focused | ReactJS | React Native | NodeJS | AWS

2mo

Very interesting! Thanks for sharing

Like
Reply
Patrick Cunha

Lead Fullstack Engineer | Typescript Software Engineer | Nestjs | Nodejs | Reactjs | AWS

2mo

Cool! Looking forward to checking it out.

Like
Reply
Vinicius Rodrigues

Senior Backend NodeJS | Speaker at Google GDG for AI | Kotlin | MVVM | Fastify | Coroutines | Koin | SOLID | Unit Tests | Instrumented Tests | GraphQL | Knex.js

2mo

Great post!

Like
Reply
Marcel Amorim

Senior Frontend Developer | Mobile Developer | React | React Native | Flutter | Fastlane

3mo

Golang’s goroutines are a game-changer for concurrent programming. They’re lightweight, easy to use, and managed by Go’s runtime, making parallel execution much simpler compared to traditional threads. Using goroutines with WebSockets, as in your example, keeps the main program responsive while processing messages in the background. Looking forward to your take on channels!

Igor Matsuoka

Full Stack Engineer | React.js | Node.js | NextJS | AWS

3mo

Nice article Renan Machado Pinho!

To view or add a comment, sign in

More articles by Renan Machado

  • Java Virtual Threads Overview

    Virtual threads were first introduced as a preview feature in Java 19. They are part of Project Loom, a long-running…

    4 Comments
  • How i learn new programming Languages

    Every developer in some moment of you carrer need learn a new programming language for some random( or not ) reason…

    18 Comments
  • Explaining More About Goroutines and Channels

    In the last article, we talked about Goroutines and how easy they are to use. Now, let's delve deeper into Goroutines…

    20 Comments

Insights from the community

Others also viewed

Explore topics