Logo

Programming-Idioms

History of Idiom 91 > diff from v23 to v24

Edit summary for version 24 by programming-idioms:
[Go] Typo in comments

Version 23

2018-12-07, 12:53:18

Version 24

2018-12-07, 12:53:46

Idiom #91 Load JSON file into struct

Read from file data.json and write its content into object x.
Assume the JSON data is suitable for the type of x.

Idiom #91 Load JSON file into struct

Read from file data.json and write its content into object x.
Assume the JSON data is suitable for the type of x.

Imports
import "encoding/json"
Imports
import "encoding/json"
Code
r, err := os.Open(filename)
if err != nil {
	return err
}
decoder := json.NewDecoder(r)
err = decoder.Decode(&x)
if err != nil {
	return err
}
Code
r, err := os.Open(filename)
if err != nil {
	return err
}
decoder := json.NewDecoder(r)
err = decoder.Decode(&x)
if err != nil {
	return err
}
Comments bubble
Create and use a *json.Decoder*
Comments bubble
Create and use a *json.Decoder
Doc URL
https://golang.org/pkg/encoding/json/#Decoder
Doc URL
https://golang.org/pkg/encoding/json/#Decoder
Demo URL
https://play.golang.org/p/e5nQy3LKaur
Demo URL
https://play.golang.org/p/e5nQy3LKaur