Skip to content

Instantly share code, notes, and snippets.

@krak3n
Last active September 12, 2017 10:35
Show Gist options
  • Save krak3n/9ea4c441ccdf0a36a3ed2e4d3b9fe7db to your computer and use it in GitHub Desktop.
Save krak3n/9ea4c441ccdf0a36a3ed2e4d3b9fe7db to your computer and use it in GitHub Desktop.
Testing using Golden files in Go
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
)
func ToJSON(w io.Writer) error {
return json.NewEncoder(w).Encode(&struct {
Foo string `json:"foo"`
Bar string `json:"bar"`
}{
"Foo",
"Bar",
})
}
func main() {
var b bytes.Buffer
w := bufio.NewWriter(&b)
if err := ToJSON(w); err != nil {
fmt.Fprintln(os.Stderr, "error writing json: %s", err)
}
w.Flush()
fmt.Println(string(b.Bytes()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment