goroutine channels over a for loop

1000 views Asked by At

My main function reads json from a file, unmarshals it into a struct, converts it into another struct type and spits out formatted JSON through stdout.

I'm trying to implement goroutines and channels to add concurrency to my for loop.

func main() {
    muvMap := map[string]string{"male": "M", "female": "F"}
    fileA, err := os.Open("serviceAfileultimate.json")
    if err != nil {
        panic(err)
    }
    defer fileA.Close()

    data := make([]byte, 10000)
    count, err := fileA.Read(data)
    if err != nil {
        panic(err)
    }

    dataBytes := data[:count]

    var servicesA ServiceA
    json.Unmarshal(dataBytes, &servicesA)
    var servicesB = make([]ServiceB, servicesA.Count)
    goChannels := make(chan ServiceB, servicesA.Count)

    for i := 0; i < servicesA.Count; i++ {
        go func() {
            reflect.ValueOf(&servicesB[i]).Elem().FieldByName("Address").SetString(Merge(&servicesA.Users[i].Location))
            reflect.ValueOf(&servicesB[i]).Elem().FieldByName("Date_Of_Birth").SetString(dateCopyTransform(servicesA.Users[i].Dob))
            reflect.ValueOf(&servicesB[i]).Elem().FieldByName("Email").SetString(servicesA.Users[i].Email)
            reflect.ValueOf(&servicesB[i]).Elem().FieldByName("Fullname").SetString(Merge(&servicesA.Users[i].Name))
            reflect.ValueOf(&servicesB[i]).Elem().FieldByName("Gender").SetString(muvMap[servicesA.Users[i].Gender])
            reflect.ValueOf(&servicesB[i]).Elem().FieldByName("Phone").SetString(servicesA.Users[i].Cell)
            reflect.ValueOf(&servicesB[i]).Elem().FieldByName("Username").SetString(servicesA.Users[i].Username)
            goChannels <- servicesB[i]
        }()
    }

    for index := range goChannels {
        json.NewEncoder(os.Stdout).Encode(index)
    }

}

It compiles but is returning messages like:

goroutine 1 [chan receive]: main.main() C://.....go.94 +0x55b.
1

There are 1 answers

4
evanmcdonnal On BEST ANSWER

You're printing the channels info, not the data it contains. You don't want a loop, you just want to receive then print.

   json := <-index
   json.NewEncoder(os.Stdout).Encode(json)

Now I do I need to point out, that code is not going to block. If you want to keep reading until all work is done you need some kind of locking/coordination mechanism.

You'll often see things like

for {
   select {
        case json := <-jsonChannel:
            // do stuff
        case <-abort:
            // get out of here 
   }
}

To deal with that. Also, just fyi you're initializing your channel with a default capacity (meaning it's a buffered channel) which is pretty odd. I'd recommend reviewing some tutorials on the topic cause overall your design needs some work actually be an improvement of non-concurrent implementations. Lastly you can find libraries to abstract some of this work for you and most people would probably recommend you do. Here's an example; https://github.com/lytics/squaredance