-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Allen Mills edited this page Apr 21, 2016
·
1 revision
- Example: Opening a stream
fs := filestream.NewFileStream() // First, we need a filestream for communication.
go filestream.StartStream("output.txt", &fs) // Next, we begin a stream with file 'output.txt' with fs as our writer.
streamErr := <-fs.OK // It is required that you block until an error or nil is received through .OK
HandleError(streamErr) // If nil is returned through .OK the stream is alive and waiting for information through .Write
- Example: Write to a stream
In this example, FS is a global variable shared among different modules and a stream has been created.
FS.Write <- "String's are sent through this channel into a file."
- Example: Closing of a stream
It should be noted that the File.Close() is deferred by the StartStream function before OK gives the all clear. You do not need to use the .Quit functionality for simple use.
In this example, FS is a global variable shared among different modules and a stream has been created.
fs := filestream.NewFileStream()
go filestream.StartStream("output.txt", &fs)
<-fs.OK // this is a short way of waiting without actually checking that the file is open.
fs.Write <- "Output for file 1." // write out to the file.
fs.Close <- 0 // Stream should now be assumed close. Attempting to write will create a deadlock.
// We can use the fs variable still.
<-fs.OK // block until receiving ErrStreamClosed.
go filestream.StartStream("output2.txt", &fs) // now that the stream is in a stable state, we can assign it to another file.
<-fs.OK // wait for the ok.
fs.Write <- "Output for file 2!" // free and open for writing again.
Filestream currently has only one error to check for: ErrStreamClosed. As the documentation explains, this error is passed when the stream is explicitly closed by passing information to FileStream.Quit.
- Writing or accessing any channel of a FileStream object before it is ready will result in a deadlock. I've attempted to make note of every time this has occurred for me and solutions for such an event but if you are deadlocking, check for this behavior in your program.