Solution: Digital signature check
- [Instructor] What we need to do is we need to convert this ValidateSigs which is a sequential code to a concurrent one. And this time we need a return value from the goroutines. We're not allowed to touch this function, the sha1Sig function. So we start with what the goroutine should return and it returns it over a channel. So I'm going to say the type reply is struct and now we need to give the context. So we need the FileName, which is the string. I'm going to do a Boolean whether there was a match or not. And I'm going to say also if there was an error. So this is what the goroutine returns and now when we get it over a channel, we know exactly how to deal. Now I'm going to write my sigWorker. And the sigWorker is the goroutine that is going to happen. You can do it in an anonymous function inside the main loop, but I decided to do it here. So we going to pass it a file and a channel to return the values and we say that this is a channel that it should send to and then replies. And now we call the sha1Sig on the file.Content and now we construct the reply. So we have a reply where the fileName is the file.Name. The match is whether the signature that we got equal the file.Signature and err is the error that we got. And finally we send it back on the channel, right? So now we have this worker, it's pretty simple code and now we can work out. Now we need to split this code into two. One is the fan out part, so we're going to do fan out. And what we're going to do is first we need to create a channel. Okay, so make chan of replies and now we launch a goroutine with the sigWorker with the file and the channel. Next we need to collect the results. Okay, so I need to get end results where n is the length of the file. So I'm going to use for range in files which does exactly that. Now here, instead of getting the signature and the error, I'm receiving from the channel. Okay, so now I have that and now I can do if not r.match or and we can delete this. r.error does not equal nil. We get the file name and now we have it in r.fileName. Otherwise, and again this is going to be in r.fileName. Okay, so fan out collecting results from the channel. And finally return what we want. Let's test the code and it looks like we're good and we launched 18 goroutines to do that. You're going to see two bad files and this is intentional.