Logo

Programming-Idioms

History of Idiom 91 > diff from v22 to v23

Edit summary for version 23 by programming-idioms:
New Go implementation by user [programming-idioms]

Version 22

2018-09-11, 22:48:17

Version 23

2018-12-07, 12:53:18

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"
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*
Doc URL
https://golang.org/pkg/encoding/json/#Decoder
Demo URL
https://play.golang.org/p/e5nQy3LKaur