How to read multiple JSON objects from one file in Go -




how can read json file there 2 different objects using unmarshal ?

json example:

this structure corresponding json file.

{   "mysql": {     "address": "127.0.0.1",     "port": "3306",     "user": "user",     "password": "password",     "database": "database"   },   "postgres": {     "address": "127.0.0.2",     "port": "3306",     "user": "user2",     "password": "password2",     "database": "database2"   } } 

code snippet:

type database struct {     address  string     port     string     user     string     password string     database string } type mysql struct {     database } type postgres struct {     database } 

to this, need wrap mysql , postgres structure configuration structure pass unmarshal function:

type configuration struct {     mysql    mysql     postgres postgres }  func main() {     content, err := ioutil.readfile(confpath)     var conf configuration     err = json.unmarshal(content, &conf) } 

see full working example: https://play.golang.org./p/7ctalgsjk3

hope people.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -