I have a small problem. I am trying have a program read from a file and write the exact same thing in a different file. I have made it working but I figured it should go faster if I modified it slightly making use of goroutines. I've separated reading and writing into different functions and tried to make them communicate. They seem to communicate alright, but the writing function doesn't seem to actually be writing anything to the destination file. Thing is, when I pick a much larger file than a simple short text file, it does write, but not the entire thing, it misses stuff. Can anyone tell me exactly how this could go wrong? My code is here:
func main() {
// Creating channels
messages := make(chan byte)
status := make(chan bool)
finished := make(chan bool)
fmt.Printf("Reporting from main thread. Created the channels, go routines will follow.\n")
go readFileRoutine("file", messages, status, finished)
fmt.Printf("Reporting from main thread, readFileRoutine has been let go.\n")
go writeFileRoutine("destination", messages, status)
fmt.Printf("Reporting from main thread, writeFileRoutine has been let go. Waiting on status message\n")
<-finished
close(status)
fmt.Printf("Success!\n")
}
func readFileRoutine(filename string, messages chan byte, status chan bool, finished chan bool) {
file, err := os.Open(filename)
check(err)
buff := bufio.NewReader(file)
fmt.Printf("Reporting from readFileRoutine, initialised. Will start reading now!\n")
for {
byti, err := buff.ReadByte()
fmt.Printf("Read character %c\n", byti)
if err == io.EOF { //If EOF break for loop and finish off
readFinished = true
break
}
<-status // Waiting for status message from writeFileRoutine
messages <- byti //Putting next message on channel, then buffering next character
}
fmt.Printf("Reporting from readFileRoutine, finished reading and putting messages on channel, will now wait on writer to finish\n")
//fmt.Printf("Reporting from readFileRoutine, finished with reading. Closing file and messages channel now\n")
file.Close()
<-status
fmt.Printf("Reporting from readFileRoutine, moving on to closing messages channel. Received status from writer correctly!\n")
close(messages)
<-status
fmt.Printf("Reporting from readFileRoutine, closed messages channel, will report status on the finished channel.\n")
finished <- true
}
func writeFileRoutine(filename string, messages chan byte, status chan bool) {
file, err := os.Create(filename) // Creates a new file
check(err)
buf := bufio.NewWriter(file) //New file writer
fmt.Printf("Reporting from writeFileRoutine, initialised. Will start writing now!\n")
status <- true
for readFinished == false {
bit := <-messages // Wait for receiving
fmt.Printf("Received message %c\n", bit)
buf.WriteByte(bit)
fmt.Printf("Written byte %c\n", bit)
status <- true //Sending status to readFileRoutine, done writing and waiting for next character
}
fmt.Printf("Reporting from writeFileRoutine, read all messages from reader and put them in a file\n")
file.Close()
fmt.Printf("Reporting from writeFileRoutine, closed file, will put true on status channel and exit\n")
status <- true
}
file.Close()
(or ideally make use ofdefer
statements for clean-up) – Martin Gallagher