On Sun, May 24, 2020 at 11:05 PM <[email protected]> wrote:
> Yeah this works, but you can say this as workaround, what i really want
is, does native go support? if not why?
>> On Monday, May 25, 2020 at 7:57:17 AM UTC+5:30, tokers wrote:
>> You may try to inspect this go package: https://github.com/jtolio/gls
Each package has a set of global variables. Adding another set of globals
for each executing Goroutine would be too complex. Functions are not
specific to Goroutine, but a second global pool would require additional
considerations specific to goroutine vs main.
What is the drawback to passing the goroutine global variables as a struct
to each function? It's certainly far less overhead than the workaround and
it follows standard GO conventions.
<pre>
package main
import (
"fmt"
"time"
"sync"
)
func main() {
go goroutine()
go goroutine()
go goroutine()
go goroutine()
time.Sleep(time.Second)
fmt.Println("all done")
}
type Global struct {
Id int
Name string
Cnt int
}
var goroutineCount int = 0
func goroutine() {
mutex := &sync.Mutex{}
mutex.Lock()
goroutineCount++
mutex.Unlock()
g := Global{
Id : goroutineCount,
Name : "some name",
Cnt : 100,
}
g.Method1()
}
func (g *Global) Method1() {
g.Method2()
g.Cnt++
fmt.Println("method1", g.Id, g.Name, g.Cnt)
}
func (g *Global) Method2() {
g.Cnt++
fmt.Println("method2", g.Id, g.Name, g.Cnt)
}
</pre>
Jon.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/CAByJhJnn6uLbcM_JBBV%3DJewBQB5iztb23BYHAO98Bvf2nTkPUQ%40mail.gmail.com.