-
-
Save mottaquikarim/f753b34413ac6e36567f548d55beed61 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
// This example uses a Decoder to decode a stream of distinct JSON values. | |
func ExampleDecoder() { | |
const jsonStream = ` | |
{"Name": "Ed", "Text": "Knock knock."} | |
{"Name": "Sam", "Text": "Who's there?"} | |
{"Name": "Ed", "Text": "Go fmt."} | |
{"Name": "Sam", "Text": "Go fmt who?"} | |
{"Name": "Ed", "Text": "Go fmt yourself!"} | |
` | |
type Message struct { | |
Name, Text string | |
} | |
dec := json.NewDecoder(strings.NewReader(jsonStream)) | |
for { | |
var m Message | |
if err := dec.Decode(&m); err == io.EOF { | |
break | |
} else if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%s: %s\n", m.Name, m.Text) | |
} | |
// Output: | |
// Ed: Knock knock. | |
// Sam: Who's there? | |
// Ed: Go fmt. | |
// Sam: Go fmt who? | |
// Ed: Go fmt yourself! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment