Last active
September 12, 2017 10:35
-
-
Save krak3n/9ea4c441ccdf0a36a3ed2e4d3b9fe7db to your computer and use it in GitHub Desktop.
Testing using Golden files in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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