puneet55667788
puneet55667788

Reputation: 89

How to assign the values to struct while go routines are running?

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.

Upvotes: 1

Views: 3480

Answers (2)

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

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}

Upvotes: 2

icza
icza

Reputation: 417867

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

Upvotes: 4

Related Questions