This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Encoding/json" "FMT" "OS") Type Configstructstruct{Hoststring' JSON:"Host"' Portint' JSON:"Port"' Analyticsfilestring' JSON:"Analytics_file"' Staticfileversionint' JSON:"Static_file_version"' Staticdirstring' JSON:"Static_dir"' TemplatesDirstring' JSON:"Templates_dir"' Sertcpsockethoststring' JSON:"Sertcpsockethost"' Sertcpsocketportint' JSON:"Sertcpsocketport"' Fruits []string' JSON:"Fruits"'}type Otherstruct{Sertcpsockethoststring' JSON:"Sertcpsockethost"' Sertcpsocketportint' JSON:"Sertcpsocketport"' Fruits []string' JSON:"Fruits"'}func main () {jsonstr: = ' {"Host":"http://localhost:9090","Port":9090,"Analytics_file":"","Static_file_version":1,"Static_dir":"e:/project/gotest/src/","Templates_dir":"e:/project/gotest/src/templates/","Sertcpsockethost":": 12340","Sertcpsocketport":12340,"Fruits": ["Apple","Peach"]}`//json str goto map varDAT map[string]Interface{}ifERR: = json. Unmarshal ([]byte(JSONSTR), &dat); Err = = Nil {fmt. Println ("==============json str turn map=======================") FMT. PRINTLN (DAT) fmt. Println (dat["Host"]) }//json str transfer struct varConfig configstructifERR: = json. Unmarshal ([]byte(JSONSTR), &config); Err = = Nil {fmt. Println ("================json str turn struct==") FMT. Println (config) fmt. Println (config. Host)}//json str to struct (partial field) varPart otherifERR: = json. Unmarshal ([]byte(JSONSTR), &part); Err = = Nil {fmt. Println ("================json str turn struct==") FMT. Println (part) FMT. Println (part. Sertcpsocketport)}//struct to JSON str ifB, err: = json. Marshal (config); Err = = Nil {fmt. Println ("================struct to JSON str==") FMT. Println (string(b))}//map to JSON strFmt. Println ("================map to JSON str=====================") ENC: = json. Newencoder (OS. Stdout) Enc. Encode (DAT)//array to JSON strArr: = []string{"Hello","Apple","Python","Golang","Base","Peach","Pear"} lang, err: = json. Marshal (arr)ifErr = = Nil {fmt. Println ("================array to JSON str==") FMT. Println (string(Lang)) }//json to []string varWO []string ifERR: = json. Unmarshal (lang, &wo); Err = = Nil {fmt. Println ("================json to []string==") FMT. Println (wo)}}