Thanx. This i thought but i want context as "Global" struct, which i cant 
do.
Anyhow please refer https://github.com/golang/go/issues/21355


On Wednesday, May 27, 2020 at 6:34:24 AM UTC+5:30, Jon Perryman wrote:
>
>
> On Sun, May 24, 2020 at 11:05 PM <adithya...@gmail.com <javascript:>> 
> 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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/88474b1b-827e-4dcb-a56e-b942fafe71b8%40googlegroups.com.

Reply via email to