A
A
Alexey Ulianov2017-08-12 13:19:33
go
Alexey Ulianov, 2017-08-12 13:19:33

How to synchronize routines in golang?

for results.Next() {
var amx Amx_servers
// for each row, scan the result into our tag composite object
err = results.Scan(&amx.Id, &amx.Serverip, &amx.Serverport)
if err != nil {
amx.Serverport = 0
}
//fmt.Println(amx.Serverip, amx.Serverport)
go gol(amx.Serverip, amx.Serverport, amx.Id)

//fmt.Println(amx.Id)

}

In a loop, I call the gol function through the go routine.
The loop outputs 10000 records. He brings them out in a couple of seconds.
But the gol function needs to do some things. Connect to the server, get info. Plus, if the server does not respond, the timeout is 3 seconds. In general, not everything from the list is fulfilled.
So, how to implement synchronization, so that until all the routines are completed, the function does not close or stop working correctly, correct if it is not expressed that way. I am new to GO

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uvelichitel, 2017-08-12
@id_715

sync.WaitGroup from stdlib

func WaitForWorkers(){
    var wg sync.WaitGroup
    for range{
        ...
        wg.Add(1)
        go gol(...)
    }
    wg.Wait()
}
func gol(...){
    ...
    wg.Done()
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question