Go的结构的Jsonify映射 [英] Jsonify map of structs in Go

查看:238
本文介绍了Go的结构的Jsonify映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以现在我有一个客户端连接的结构,如下所示。

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
}

我还有一个ClientConn的地图如下

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

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

所以我在每个连接上添加一个新的ClientConn到clientList但是我想要做的是jsonify clientList,并获取一个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.

如果我做

json.Marshal(clientList)

然后我只是得到一个空的对象的键,我想要检索的是整个ClientConn结构数组的值和键。

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.

这将是一种方法?

推荐答案

这是 go 标签

您的结构字段必须导出,也就是以大写字母开头。

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

在官方博客上, JSON和Go 可以很好地解释json。

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

对Go有兴趣的人必读:有效Go

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

这篇关于Go的结构的Jsonify映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆