简体   繁体   English

Go中的Jsonify结构图

[英]Jsonify map of structs in Go

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 而且我还得到了ClientConn的地图,如下所示

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. 因此,我在与clientList的每个连接上添加了一个新的ClientConn,但是我想做的是对clientList进行json处理,并获取一个ClientConn数组,其中包含其值,而不仅仅是键。

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. 然后我只是得到一个带有空对象的键,而我想要检索的是带有值和键的整个ClientConn结构数组。

What would be a way to do this? 有什么方法可以做到这一点?

This is the daily question of the tag. 这是标签的日常问题。

Your struct fields has to be exported, aka start with an uppercase letter. 您的struct字段必须以大写字母开头。

A good read to explain json with go is JSON and Go on the official blog. 用go解释json的不错的读物是官方博客上的JSON和Go

A must-read for anyone interested in Go is Effective Go . 对Go感兴趣的任何人必读的书是Effective Go

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM