json - goroutine channels over a for loop -


my main function reads json file, unmarshals struct, converts struct type , spits out formatted json through stdout.

i'm trying implement goroutines , channels add concurrency 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)      := 0; < 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]         }()     }      index := range gochannels {         json.newencoder(os.stdout).encode(index)     }  } 

it compiles returning messages like:

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

you're printing channels info, not data contains. don't want loop, want receive print.

   json := <-index    json.newencoder(os.stdout).encode(json) 

now need point out, code not going block. if want keep reading until work done need kind of locking/coordination mechanism.

you'll see things

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

to deal that. also, fyi you're initializing channel default capacity (meaning it's buffered channel) pretty odd. i'd recommend reviewing tutorials on topic cause overall design needs work improvement of non-concurrent implementations. lastly can find libraries abstract of work , people recommend do. here's example; https://github.com/lytics/squaredance


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -