The following steps cover how to write and run your application:
- From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter1/interfaces.
- Navigate to this directory.
- Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter1/interfaces
You should see a file called go.mod that contains the following:
module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter1/interfaces
- Copy tests from ~/projects/go-programming-cookbook-original/chapter1/interfaces or use this as an exercise to write some of your own code!
- Create a file called interfaces.go with the following contents:
package interfaces
import (
"fmt"
"io"
"os"
)
// Copy copies data from in to out first directly,
// then using a buffer. It also writes to stdout
func Copy(in io.ReadSeeker, out io.Writer) error {
// we write to out, but also Stdout
w := io.MultiWriter(out, os.Stdout)
// a standard copy, this can be dangerous if there's a
// lot of data in in
if _, err := io.Copy(w, in); err != nil {
return err
}
in.Seek(0, 0)
// buffered write using 64 byte chunks
buf := make([]byte, 64)
if _, err := io.CopyBuffer(w, in, buf); err != nil {
return err
}
// lets print a new line
fmt.Println()
return nil
}
- Create a file called pipes.go with the following contents:
package interfaces
import (
"io"
"os"
)
// PipeExample helps give some more examples of using io
//interfaces
func PipeExample() error {
// the pipe reader and pipe writer implement
// io.Reader and io.Writer
r, w := io.Pipe()
// this needs to be run in a separate go routine
// as it will block waiting for the reader
// close at the end for cleanup
go func() {
// for now we'll write something basic,
// this could also be used to encode json
// base64 encode, etc.
w.Write([]byte("test\n"))
w.Close()
}()
if _, err := io.Copy(os.Stdout, r); err != nil {
return err
}
return nil
}
- Create a new directory named example and navigate to it.
- Create a main.go file with the following contents:
package main
import (
"bytes"
"fmt"
"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter1/bytestrings"
)
func main() {
in := bytes.NewReader([]byte("example"))
out := &bytes.Buffer{}
fmt.Print("stdout on Copy = ")
if err := interfaces.Copy(in, out); err != nil {
panic(err)
}
fmt.Println("out bytes buffer =", out.String())
fmt.Print("stdout on PipeExample = ")
if err := interfaces.PipeExample(); err != nil {
panic(err)
}
}
- Run go run ..
- You may also run the following:
$ go build
$ ./example
You should see the following output:
$ go run .
stdout on Copy = exampleexample
out bytes buffer = exampleexample
stdout on PipeExample = test
- If you copied or wrote your own tests, go up one directory and run go test, and ensure that all tests pass.