Here’s the translation of the Go code to Haskell, formatted in Markdown suitable for Hugo:
Writing files in Haskell follows similar patterns to the ones we saw earlier for reading.
Try running the file-writing code:
Then check the contents of the written files:
In this Haskell version:
We use Data.ByteString for efficient byte operations.
The withFile function is used to safely open and close files.
hPutStrLn is used instead of WriteString.
hFlush is used to flush the buffer, similar to Sync in the original example.
hSetBuffering is used to set up buffered I/O.
Note that Haskell’s strong type system and functional nature lead to some differences in how file I/O is handled compared to more imperative languages.
Next, we’ll look at applying some of the file I/O ideas we’ve just seen to the stdin and stdout streams.