Jsonify map of structs in Go

巧了我就是萌 提交于 2019-12-23 05:08:09

问题


So right now I have a struct for client connections which looks as following

type ClientConn struct {
    uuid      string
    websocket *websocket.Conn
    ip        net.Addr
    longitude float64
    latitude  float64
}

and I've also got a map of ClientConn as following

var clientList = make(map[string]*ClientConn)

so I add a new ClientConn on each connection to the clientList but what I'm trying to do is jsonify the clientList and obtain an array of ClientConn with its values and not just keys.

If I do

json.Marshal(clientList)

then I just get the keys with a empty object and what I'd like to retrieve is the whole ClientConn struct array with the values and keys.

What would be a way to do this?


回答1:


This is the daily question of the go tag.

Your struct fields has to be exported, aka start with an uppercase letter.

A good read to explain json with go is JSON and Go on the official blog.

A must-read for anyone interested in Go is Effective Go.



来源:https://stackoverflow.com/questions/26281366/jsonify-map-of-structs-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!