Created
April 29, 2015 14:12
-
-
Save eikaas/c236e1e04ef8dad23839 to your computer and use it in GitHub Desktop.
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 "os" | |
import "io" | |
import "encoding/json" | |
type MyWriters struct { | |
StandardOutput io.Writer | |
StandardError io.Writer | |
FileOutput io.Writer | |
Everything io.Writer | |
} | |
func main() { | |
myFileWriter, err := os.Create("output.json") | |
defer myFileWriter.Close() | |
if err != nil { | |
os.Exit(1) | |
} | |
out := MyWriters{ | |
StandardOutput: os.Stdout, | |
StandardError: os.Stderr, | |
FileOutput: myFileWriter, | |
} | |
out.Everything = io.MultiWriter(os.Stdout, os.Stderr, myFileWriter) | |
json.NewEncoder(out.Everything).Encode(struct { | |
Name string `json: "name"` | |
Age int `json: "age"` | |
}{ | |
"Hasse Laugen", | |
30, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment