1

I'm using goroutines in my project and I want to to assign the values to the struct fields but I don't know that how I will assign the values get by using mongodb quires to the struct fields I'm showing my struct and the query too.

type AppLoadNew struct{
    StripeTestKey      string                   `json:"stripe_test_key" bson:"stripe_test_key,omitempty"`
    Locations          []Locations              `json:"location" bson:"location,omitempty"`
}

type Locations struct{
   Id int `json:"_id" bson:"_id"`
   Location  string `json:"location" bson:"location"`
}

func GoRoutine(){
   values := AppLoadNew{}
   go func() {
      data, err := GetStripeTestKey(bson.M{"is_default": true})
      if err == nil {
        values.StripeTestKey := data.TestStripePublishKey
      }
  }()
  go func() {
      location, err := GetFormLocation(bson.M{"is_default": true})
      if err == nil {
        values.Locations := location
      }
  }()
  fmt.Println(values) // Here it will nothing
  // empty
}

Can you please help me that I will assign all the values to the AppLoadNew struct.

icza
  • 389,944
  • 63
  • 907
  • 827
puneet55667788
  • 89
  • 2
  • 10
  • You're ignoring errors. Those values will be empty if there was an error in your queries, but you're not examining the returned error values. – Adrian Jan 31 '19 at 14:14

2 Answers2

3

In Go no value is safe for concurrent read and write (from multiple goroutines). You must synchronize access.

Reading and writing variables from multiple goroutines can be protected using sync.Mutex or sync.RWMutex, but in your case there is something else involved: you should wait for the 2 launched goroutines to complete. For that, the go-to solution is sync.WaitGroup.

And since the 2 goroutines write 2 different fields of a struct (which act as 2 distinct variables), they don't have to be synchronized to each other (see more on this here: Can I concurrently write different slice elements). Which means using a sync.WaitGroup is sufficient.

This is how you can make it safe and correct:

func GoRoutine() {
    values := AppLoadNew{}

    wg := &sync.WaitGroup{}

    wg.Add(1)
    go func() {
        defer wg.Done()
        data, err := GetStripeTestKey(bson.M{"is_default": true})
        if err == nil {
            values.StripeTestKey = data.StripeTestKey
        }
    }()

    wg.Add(1)
    go func() {
        defer wg.Done()
        location, err := GetFormLocation(bson.M{"is_default": true})
        if err == nil {
            values.Locations = location
        }
    }()

    wg.Wait()
    fmt.Println(values)
}

See a (slightly modified) working example on the Go Playground.

See related / similar questions:

Reading values from a different thread

golang struct concurrent read and write without Lock is also running ok?

How to make a variable thread-safe

icza
  • 389,944
  • 63
  • 907
  • 827
  • can you tell me please that what is the purpose of `wg := &sync.WaitGroup{}` also `wg.Add(1)`. – puneet55667788 Jan 31 '19 at 10:17
  • Now I'm using 1 go routine for all quires but in go routine I pass all the values into the different function which return the variable assigned all the values to the struct and How i will access that variable? – puneet55667788 Jan 31 '19 at 10:25
  • @puneet55667788 `wg := ...` creates a new variable called `wg` which will be a `WaitGroup`. `wg.Add(1)` adds one to the wait group's counter, which tells how many times `wg.Done()` have to be called in order to a call to `wg.Wait()` be unblocked. This is used here to make the main goroutine wait all spawned goroutines before it proceeds printing the value of `values`. – icza Jan 31 '19 at 14:25
2

You can use sync package with WaitGroup, here is an example:

package main

import (
    "fmt"
    "sync"
    "time"
)

type Foo struct {
    One string
    Two string
}

func main() {
    f := Foo{}
    var wg sync.WaitGroup

    wg.Add(1)
    go func() {
        defer wg.Done()
        // Perform long calculations
        <-time.After(time.Second * 1)
        f.One = "foo"
    }()

    wg.Add(1)
    go func() {
        defer wg.Done()
        // Perform long calculations
        <-time.After(time.Second * 2)
        f.Two = "bar"

    }()

    fmt.Printf("Before %+v\n", f)
    wg.Wait()
    fmt.Printf("After %+v\n", f)
}

The output:

Before {One: Two:}
After {One:foo Two:bar}
WeizhongTu
  • 6,124
  • 4
  • 37
  • 51
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103