[Music] concurrency in programming is about handling multiple tasks independently go makes concurrency easy thanks to its built-in support for two powerful features go routines and channels let me quickly show you how they work say I want to read in a bunch of files and I've got the names in a slice plus a function that knows how to process a single file I could just call Process file for the first file wait for it to finish call again for the second wait some more and then do the third one but
because file.io is synchronous and slow that's probably not the fastest way to do it let's use go to make those three calls concurrently process file can either return an error or if everything goes right nil I'll make a channel to collect those results think of a channel as a pipe with one end you can send to and another you can receive from now I'll Loop over the files and use the go statement to start a go routine for each one you can think of a go routine as a very efficient thread as each c
all to process file completes the returned errors and Nils are sent to the channel I'll end up with one return value for each file so I can do a little Loop here and print out each error you might be wondering with stuff happening concurrently how do you make sure the print code doesn't try to print a value before the process file call finishes that's part of what makes channel so useful when this code tries to receive the value from the channel it will wait until one arrives then resume executi
ng and that's it for more info on how to use go routines and channels to simplify asynchronous coding in your own applications see the links [Music] below
Comments
🙋 Do you have any concurrency questions? Let us know below!