Questions tagged [goroutine]
A goroutine is a lightweight thread of execution that is managed by the Go language runtime.
1,495
questions
-2
votes
0
answers
45
views
GOLANG simple client can't connect to simple server using goroutines in windows [duplicate]
I have simple client/server app that is using goroutines
client:
func main() {
conn, err := net.Dial("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
...
1
vote
2
answers
66
views
Does a goroutine necessarily run on a different CPU?
The following excerpt is from https://go.dev/doc/effective_go#parallel.
We launch the pieces independently in a loop, one per CPU. They can complete in any order but it doesn't matter; we just count ...
-1
votes
0
answers
49
views
Optimization for loop in golang [closed]
I have array of data of many station that has two destination.
cases:
Get total of exact station that's can bring you to your destination.
Get total alternative station route to your destination.
...
-1
votes
1
answer
34
views
Why might a program crash on incorrect synchronization? (Go) [closed]
The following excerpt is from "The Go Memory Model" (at https://go.dev/ref/mem):
A receive from an unbuffered channel happens before the send on that channel completes.
This program (as ...
2
votes
2
answers
47
views
Track progress of long running tasks - correct approach
I'd like to track execution of some long running process and show the user completion percentage and errors (if any). If it's one long running process, then it's easy - you can create channels for ...
0
votes
0
answers
45
views
How To Stop Sending To A Channel Once Context Is Canceled
I face an error while working with channel and context in golang.
So my idea is,
I want to run 3 functions with goroutines and pass a channel into that function. If one of that functions is already ...
-2
votes
0
answers
23
views
Terminate a goroutine [duplicate]
I am new to Go and to multi- threaded programming and I'm trying to operate a socket server in Go. The server works like a cache, until it gets a command from the client to exit.
Basically, the part ...
-1
votes
1
answer
56
views
Why is "Greetings done" getting printed before "Bonjour" in the below example of goroutines and channels?
In the below example, Why is "Greetings done" getting printed before "Bonjour" ? Shouldn't the sender routine wait until receiver routine receives the value in channel ?
package ...
-1
votes
1
answer
46
views
Idiomatic goroutine concurrency and error handling
In the below code block I am trying to run several routines and get results (Whether success or error) for all of them.
package main
import (
"fmt"
"sync"
...
-1
votes
1
answer
48
views
Is there any other way that goroutines can communicate with eachother, Other than channels? [closed]
I had been interview today and one of the interesting that i come across from the interviewer is we know that Channels are the medium through which goroutines can communicate with each other.
But, ...
0
votes
1
answer
42
views
Go dependent functions / nested functions best practices with context
Suppose I have a go program that executes a long running job (generally as a goroutine), where there are many steps and each step is dependent on the completion of the last, i.e.
A -> B -> C -&...
1
vote
3
answers
97
views
goroutine not seeing context cancel?
I have two goroutines running at the same time.
At some point, I want my program to exit gracefully so I use the cancel() func to notify my goroutines that they need to be stopped, but only one of the ...
0
votes
2
answers
74
views
Does using `runtime.Gosched()` in the default case of a Select statement make any sense?
Go's documentation says that
Gosched yields the processor, allowing other goroutines to run. It does not suspend the current goroutine, so execution resumes automatically.
Based on that definition ...
-4
votes
1
answer
65
views
Non-blocking sleep in go
In C# when we want to cause delay between the execution of two procedures we have two options:
Thread.Sleep(time) which is blocking
await Task.Delay(time) which is non-blocking
In other words, ...
0
votes
1
answer
80
views
golang goroutine practice, function or channel?
constantly receive Json data from websocket and process them in goroutine, no idea is this writing pattern is encourage or not
ws.onmessage { //infinite receive message from websocket
...
1
vote
1
answer
77
views
What happens to unfinished goroutines when the main/parent goroutine exits or returns?
I am reading the go programming language book and there is this example in Chapter 8.4
func mirroredQuery() string{
responses := make(chan string, 3)
go func() { responses <- request("...
-1
votes
0
answers
32
views
goroutines not adding all entries to the synced map [duplicate]
I created a map to contain sum of numbers in each file with filename as key. But after running the program, it only displays some of the files even though the total sum is correct. I am unable to find ...
0
votes
0
answers
128
views
Why this server program that written in Go hangs?
This TCP server program does broadcast Candle(It represents that stock prices of Open, High, Low, Close, Datetime in for each specific period) data to clients.
I don't know but This program does hang ...
0
votes
1
answer
89
views
Limit goroutines in a loop
I need someome to help or at least any tip. I'm trying to read from large files (100mb - 11gb) line by line and then store some data into Map.
var m map[string]string
// expansive func
func stress(s ...
-2
votes
1
answer
89
views
How to implement thread-safe map of maps in golang?
I am working on a multi-threaded module and need to implement map of map in golang - map[outer]map[inner]*some_struct. The outer key(map[outer]) will be accessed by multiple threads(goroutines) to add ...
1
vote
1
answer
48
views
How to tell if one goroutine succeeded or all goroutines are done?
I am trying to use DFS to check a graph for cycles by checking each node in the graph for a cycle.
I would like to run a goroutine for each node and terminate when the first cycle is detected or when ...
0
votes
1
answer
34
views
Golang timedout sending emails
I have 708 companies whom I need to send email with login and password for authorization on my Company new web-app, where they can see statistics and other information co-working with our Company. ...
2
votes
2
answers
261
views
string vs integer as map key for memory utilization in golang?
I have a below read function which is called by multiple go routines to read s3 files and it populates two concurrent map as shown below.
During server startup, it calls read function below to ...
0
votes
1
answer
66
views
I don't understand why this works with an unbuffered channel, or why a wait group is needed
In this code, I call a function which counts the number of letters in a string, and return a map of runes. To leverage concurrency, I call the function using goroutines:
func ConcurrentFrequency(l []...
0
votes
0
answers
58
views
Processing Multiple urls using go routines
I am trying to validates a list of urls using http.Get() method of Golang. The issue I am facing is there are certain urls in the list who are taking lot of time before giving timeout error.I need to ...
-2
votes
1
answer
38
views
Issue sharing array between goroutines
I am trying to solve this golang exercise https://github.com/loong/go-concurrency-exercises/tree/master/1-producer-consumer.
I guess I am close to the solution but I am getting a deadlock error
...
-4
votes
1
answer
48
views
Go:How to print dog, cat and fish alternately for 3 times through 3 goroutine without deadlock?
1.How to use sync.WaitGroup to solve deadlock?
My code can print the result correctly, but it will cause deadlock.
What I can think of now is:replace sync.WaitGroup with time.Sleep.
But I want to know ...
-1
votes
2
answers
54
views
GoLang concurrency- Main routine is never called
I am a newbie to golang. Trying to learn concurrency. The concept with the join point after forking using the channels troubles me now. THis is the code that I have in place
package main
import (
...
2
votes
1
answer
130
views
Is there a way to cancel a context after a delay after one goroutine returns?
Problem
Situation
I currently have a gin handler function that runs three separate queries in three separate goroutines using the same context. There is an err group ("golang.org/x/sync/errgroup&...
2
votes
2
answers
65
views
Recursive calls from function started as goroutine & Idiomatic way to continue caller when all worker goroutines finished
I am implementing a (sort of a) combinatorial backtracking algorithm in go utilising goroutines. My problem can be represented as a tree with a certain degree/spread where I want to visit each leaf ...
0
votes
0
answers
91
views
Read first line of go io.copy()?
I am very new to Go, I would like to make a HTTP proxy loader balancer, with URL based switching rules. Therefore I need to read the CONNECT headers. It works like this: browser -> :1234 -> :...
1
vote
2
answers
91
views
goroutines have high idle wake up calls
I'm using GoLang to run two websocket clients (one for private and one for public data) simultaneously using goroutines. On the surface, everything seems to work fine. Both clients receive data ...
1
vote
0
answers
71
views
Fyne GUI Goroutine halts after a couple of ticks
I have started to write a simple Go GUI application which - for now - only displays the current time at the top left corner. My issue is that the Goroutine which makes the clock tick stops after 2-3 ...
3
votes
4
answers
285
views
Load data from reading files during startup and then process new files and clear old state from the map
I am working on a project where during startup I need to read certain files and store it in memory in a map and then periodically look for new files if there are any and then replace whatever I had in ...
2
votes
1
answer
75
views
Golang 'defer' causing delay in sending(receiving) API response
I have created an API, which after processing the request sends a response and starts a background goroutine which logs some messages. I have used 'defer' to execute goroutine after the API request ...
1
vote
2
answers
80
views
Problems with map, goroutine and mutex
This project is made to receive POST routes that will finally count as access to later write to a database. The intuition is to save interaction with the database of another project in production. I ...
2
votes
0
answers
133
views
Why does Golang terminate the whole process if one goroutine paincs? [closed]
According to Go's specifications:
While executing a function F, an explicit call to panic or a run-time panic terminates the execution of F. Any functions deferred by F are then executed as usual. ...
3
votes
3
answers
121
views
Is it possible to cancel unfinished goroutines?
Consider a group of check works, each of which has independent logic, so they seem to be good to run concurrently, like:
type Work struct {
// ...
}
// This Check could be quite time-consuming
...
0
votes
1
answer
100
views
Which type of UML diagram is suited for depicting goroutines collaborating via channel?
Let's assume there is a simple integer calculator that only supports addition and multiplication operation. It will receive an integer generator and an integer as additive or multiplier as its input ...
0
votes
1
answer
53
views
how to know when a worker panic in golang ants
I am using ants as my goroutine pool in my program. This library handles panics of workers itself and won't propagate the panic, but I want to catch the panic whenever it occurs in one of my workers.
...
2
votes
1
answer
86
views
How to start & stop heartbeat per session using context.WithCancel?
I'm implementing currently the Golang client for TypeDB and struggle with their session based heartbeat convention. Usually, you implement heartbeat per client so that's relatively easy, just run a ...
-4
votes
2
answers
140
views
Why is WaitGroup.Wait() hanging when using it with go test? [duplicate]
Here's a simple example of what I mean
package main
import (
"sync"
"testing"
"time"
)
func TestWaitGroup(t *testing.T) {
var wg sync.WaitGroup
...
1
vote
1
answer
319
views
Loop through all files in all folders recursively as fast as possible in GOLANG
I'm facing a problem that even after spending the day on the forums I still can't quite understand and solve.
So here it is, I made a function that loops over all the folders as well as its sub-...
2
votes
1
answer
104
views
Unexpected behaviour of time.Now() in goroutine
As a way of trying to familiarize myself with Go I am trying to build a (completely unreliable) random number generator. The idea is to time 100 GET requests to some url, do something with the results ...
1
vote
0
answers
99
views
Unable to open `proc/[pid]/status` for live memory profiling of an executing command
Motive:
I have been trying to profile code execution for time as well as memory consumption and kill the executing command once a limit for either parameter is reached.
Roadblock:
I am trying to write ...
-2
votes
1
answer
53
views
goroutine in loop for map doesn't captured or append value
I have a case where I read an excel file with a third-party package, I need goroutine to do some logic read and insert them into the database during looping,
here is what I tried
var wg sync....
2
votes
1
answer
73
views
Not getting any output even if there is no race conditions
I am trying to create producer-consumer message queue system in Golang using buffered channel. Here is my implementation.
package main
import "fmt"
type MessageQueue struct {
storage ...
0
votes
0
answers
67
views
Goroutine cancellation with recovery
Without frameworks like RxGo, how could I accomplish the following in Go?:
Three goroutines of different running times are in a context: short, medium, and, long. These goroutines are long-running ...
0
votes
0
answers
65
views
Download files in parallel using go-routines and channel causing resource leak?
I have below code where I download s3 files in parallel. But my intellij IDE complains that there is a resource leak on this line defer close(chans[i]).
func (s3client *S3Client) BatchDownloadFiles(...
0
votes
0
answers
30
views
What approach is best for concurrent producers and consumers?
I have a use case where I want to have a pool of N integers (0 - N-1) shared by N workers (N <= 100), each claiming an integer from the pool, "working" (for this example sleeping for a ...