Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

DIWAKARKASHYAP
DIWAKARKASHYAP

Posted on

     

Concurrency in Go (Golang)

Go (or Golang) makes it easy to do many things at once using goroutines, channels, and the select statement. Let's break it down step by step.

1. Goroutines:

In Go, a goroutine is like a mini-task that the computer can handle. It's like having multiple helpers that can work together. Starting a goroutine is simple:

gofunctionName()
Enter fullscreen modeExit fullscreen mode

Just put the word "go" before the function you want to do at the same time. Imagine it's like telling your computer, "Hey, do this task while I do other things!"

For example:

funcsayHello(){fmt.Println("Hello from a helper!")}funcmain(){gosayHello()fmt.Println("Hello from the main task!")}
Enter fullscreen modeExit fullscreen mode

In this code, the helper says "Hello" while the main task also says "Hello." Sometimes the helper might finish first, and sometimes the main task might finish first. It's like a little race!

2. Channels:

Channels are like special tubes that help the helpers (goroutines) talk to each other. They can send and receive messages. It's like passing notes between friends.

Here's how you make a channel:

ch:=make(chanint)
Enter fullscreen modeExit fullscreen mode

This creates a channel that can carry numbers. You can send and get data from it:

gofunc(){ch<-42// Sending 42 through the channel}()value:=<-ch// Getting a value from the channelfmt.Println(value)
Enter fullscreen modeExit fullscreen mode

3. Select Statement:

The select statement is like a helper that watches multiple channels at once. It's like someone watching multiple phones for calls. When a call comes on any phone, they answer it.

For example:

select{casemsg1:=<-ch1:fmt.Println("Got",msg1)casemsg2:=<-ch2:fmt.Println("Got",msg2)casech3<-3:fmt.Println("Sent 3 to ch3")default:fmt.Println("No messages")}
Enter fullscreen modeExit fullscreen mode

This select helper listens to different channels. If a message comes on any channel, it does something. If many channels have messages, it picks one to answer. It's like a multitasking helper!

Quick Tips:

  • No Sharing Memory: Goroutines talk with channels, so we don't need to worry about sharing information directly. It's like talking through notes instead of fighting over toys.

  • Buffered Channels: Channels can hold messages before they're read. It's like a mailbox that can store letters.

  • Range and Close: Channels can be closed when no more messages will come. You can also use a range to read all messages like collecting all the candies.

  • Sync Package: There's a special tool called the sync package. It helps when things get more complicated, like when many helpers need to work together.

In Conclusion:

Go's way of doing many things at once is smart and easy. It's like having helpers, talking through channels, and using select to manage them. It's much simpler than old ways of doing many things together.

Thank you for reading. I encourage you to follow me on Twitter where I regularly share content about JavaScript and React, as well as contribute to open-source projects and learning golang. I am currently seeking a remote job or internship.

Twitter:https://twitter.com/Diwakar_766

GitHub:https://github.com/DIWAKARKASHYAP

Portfolio:https://diwakar-portfolio.vercel.app/

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
dyfet profile image
David Sugar
Improving business confidentiality in the post-quantum world.
  • Location
    Cape May
  • Joined

When talking about concurrency, I think it's important to also talk about how it relates to blocking behavior, like waiting on socket data or input, then it becomes much more clear why it is important.

CollapseExpand
 
diwakarkashyap profile image
DIWAKARKASHYAP
As a passionate developer, I thrive on acquiring new knowledge. My journey began with web development, and I am now actively engaged in open source contributions, aiding individuals and businesses.
  • Email
  • Location
    earth
  • Education
    Bsc(computer science) 3rd year
  • Pronouns
    he/him
  • Joined

thank you , i am happy to find that this blog is useful

CollapseExpand
 
philipjohnbasile profile image
Philip John Basile
Staff Full Stack Engineer | Real-Time 3D & Game Development | VR/AR | AI-Driven Experiences
  • Email
  • Location
    New Rochelle, New York
  • Education
    Bachelor of Computer Science - Fordham University
  • Pronouns
    He/Him
  • Joined

sweet

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

As a passionate developer, I thrive on acquiring new knowledge. My journey began with web development, and I am now actively engaged in open source contributions, aiding individuals and businesses.
  • Location
    earth
  • Education
    Bsc(computer science) 3rd year
  • Pronouns
    he/him
  • Joined

More fromDIWAKARKASHYAP

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp