1,792 questions
1
vote
0
answers
83
views
Golang benchmarks involving goroutines show higher than expected allocations when controlling the timer manually
Using go version go1.25.3 darwin/arm64.
The below implementation is a simplified version of the actual implementation.
type WaitObject struct{ c chan struct{} }
func StartNewTestObject(d time....
3
votes
0
answers
145
views
How to immediately stop `io.Copy(os.Stdin, stream)` when stream is closed in Go?
I’m trying to forward input from os.Stdin to a network stream in Go like this:
go func() {
for {
if _, err := io.Copy(stream, os.Stdin); err != nil {
log.Error(err)
...
-2
votes
1
answer
85
views
How to allow admin to cancel a long-running MQTT device activation in Go when using unbuffered channels? [closed]
I’m building a Go backend to control IoT motors via MQTT. Each device activation is handled asynchronously by a goroutine. Here’s the simplified flow:
A client POSTs /activate-device with device_id ...
1
vote
1
answer
58
views
Correct pattern to process ZeroMQ messages using Goroutines
I am implementing a Zero MQ message listener service in Go, and I am struggling to find the most idiomatic pattern of processing those messages and storing them in a DB using Goroutines. I am ...
1
vote
3
answers
279
views
Golang http.Client causes increasing goroutines and (socket) file descriptors
I have a Go program that runs continuously, and I've noticed that both the number of goroutines and open file descriptors increase steadily over time (daily). However, the number of active network ...
0
votes
1
answer
144
views
increasing number of goroutines when using go-co-op/gocron
I'm trying to create some cron tasks to test out the https://github.com/go-co-op/gocron library. I'm exploring the singleton mode where if the same instance of a task is being executed, then it's ...
-3
votes
3
answers
172
views
Functions look very similar. How to avoid code duplication?
I have a few convenience funcs and types in my utils package I use in several projects. They help me getting objects, array of objects from external apis via urls or custom requests (for auth api ...
6
votes
2
answers
147
views
Golang IO Race Condition
Having a very simplified example (still I'm not sure it would be totally reproducible at any env)
So there's a socket pipe
func SocketPair() (*os.File, *os.File, error) {
fds, err := syscall....
2
votes
1
answer
63
views
Can we assume that queries written with clause.Locking are transactional by default?
As I was reading the docs, I came across this line:
GORM perform write (create/update/delete) operations run inside a transaction to ensure data consistency, you can disable it during initialization ...
2
votes
1
answer
117
views
Why do coroutines need less memory than threads?
It is often said that coroutines could be thought of as "light threads" because they consume less memory. However, I wasn't able to find any explanation on why exactly that is the case. What ...
0
votes
0
answers
21
views
When using the global gorm.DB to create data in multiple goroutine, the data is overwritten partly
Env
go version 1.18
gorm v1.25.2
Code
My code in goroutine:
type TaskThread struct {
task *model.Task
jobID uint64
}
func (t *TaskThread) Start() {
go t.run()
}
func (t *...
0
votes
1
answer
117
views
Go Exit embedded goroutines
In Go I cannot figure out how to get the following setup.
1st level go routines should stop their 2nd level goroutines before they terminate.
If the functionrunGoroutine is not in a loop (I don't ...
1
vote
2
answers
121
views
Signal-only channel at the beginning of goroutine
I ran into the next piece of code (simplified) and can't understand purpose of channel check. This snippet was found right before a function end.
check := make(chan struct{}, 1)
go func() {
check &...
0
votes
2
answers
92
views
Different Behaviors in Go Race Condition Scenarios
I'm trying to understand why two similar pieces of code behave differently. Both snippets create a large number of goroutines that try to append to the same slice concurrently, which I understand is a ...
0
votes
0
answers
56
views
go func() and time.Sleep(changeableDuration * second) combine invalidates BlockInput(false)
Object
I want to write a program that can block input from both keyboard and mouse with golang. However, there is a problem that I can only block input but cannot retrieve it when I use go func(). I ...
0
votes
0
answers
73
views
"send on closed channel" Error in Go when Handling AMQP Messages with Context Timeout
I'm encountering a panic: send on closed channel error in my Go application when handling AMQP messages using context timeouts. Here is a simplified version of the code that reproduces the issue:
func ...
3
votes
1
answer
253
views
Swap message after sometime in watermill Golang
I am using Watermill to develop software where I send a message, and it goes through service1, service2, and the last service. I use a slice to control the order of the messages (FIFO, as GoChannel ...
-3
votes
2
answers
260
views
Why is using a semaphore slowing down my Go program
I've made a program that scrapes all the pages of a website using goroutines:
func main() {
start := time.Now()
knownUrls := getKnownURLs(os.Getenv("SITEMAP_URL"))
var wg sync....
0
votes
0
answers
86
views
Request Chunks from the peer in parallel in golang over a tcp connection
I'm building a P2P file transfer system in Go that fetches chunks of data from peers in parallel. While sequential requests work fine, parallel requests using goroutines behave inconsistently, ...
1
vote
1
answer
75
views
Different ways of closing a channel in Go
In "Sample 1" I close a channel inside the Wakeup goroutine.
In "Sample 2" I close a channel inside the anonymous goroutine (in main function).
If these two have exactly the same ...
-1
votes
1
answer
68
views
GO Cond - fmt.Println after wg.Done ended up dead lock
Unable to understand this dead lock situation in golang, i have below to go code with pub and sub pattern
package main
import (
"fmt"
"sync"
)
func main() {
cond := ...
-1
votes
3
answers
173
views
Golang : Collect Responses from spawned go routines [duplicate]
I need to run parallel tasks based on an input array and wait for all of them to finish and process their response.
My code waits for all the go routines to finish using wait group and then read the ...
1
vote
1
answer
332
views
Goroutine inside function
I've read https://go.dev/blog/pipelines and there're two functions:
// move numbers into a channel
func gen(nums ...int) <-chan int {
out := make(chan int)
go func() {
for _, n := ...
0
votes
0
answers
72
views
How many go routines will be created at runtime?
In an interview today, I got a problem to write a program with 2 goroutines to generate even and odd sequence and synchronize them to print the sequence of number.
Later, I was asked how many routines ...
1
vote
1
answer
618
views
Go - how to read a large file into chunks and process using multithreading, and aggregate results
I have a very large CSV file that won't fit entirely into memory. I want to be able to read the file into chunks, and then chain a series of operations together to process the results. Lastly, I need ...