The structure of the go language array is converted to a string string

Source: Internet
Author: User

Transformation Order: First convert the struct structure to a map, then change the map to a string string

struct--> map--> string

Type demo struct {
	id                    string
	Name	              string
}
func main () {

Demos: = [{"Id": "1", "Name": "Zs"} , {"id": "2", "name": "ls"},{"Id": "3", "Name": "WW"}]

for _, V: = Range Demos {
		Tmpdata: = Struct2map (v)
		str, err : = JSON. Marshal (tmpdata)
		If err!= nil {
			FMT. PRINTLN (Err)
		}		
	fmt.println (String (str)}
 }

}
//struct body to map
func struct2map (obj interface{}) map[string]interface{} {
	T: = reflect. TypeOf (obj)
	V: = reflect. valueof (obj)

	var data = make (map[string]interface{}) for
	I: = 0; i < T.numfield (); i++ {
		Data[t.field (i ). Name] = V.field (i). Interface ()
	} return
	data
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.