http - 使用计算键去构造

标签 http post go struct types

如何在 Go 中为以下数据结构创建 struct

{
    "description": String,
    "public":      Boolean,
    "files": {
        "some_filename.txt": {
            "contents": String
        }
    }
}

我从以下开始:

type File struct {
    // stuck here?
}

type Payload struct {
    Description string
    Public      bool
    Files       File
}

非常感谢将此用于 HTTP post 请求的任何帮助。

最佳答案

当 key 在编译时未知时使用映射:

type File struct {
    Contents string
}

type Payload struct {
    Description string
    Public      bool
    Files       map[string]*File
}

https://play.golang.org/p/9KNvHToXG3

关于http - 使用计算键去构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47482278/

相关文章:

c++ - 分析帖子请求

go - 是否可以在运行时检测Go二进制文件是否使用-H=windowsgui编译

ruby - 如何在 ruby​​ 中保留自定义 header 大小写

http - 这个错误是什么意思 : HPE_INVALID_CONSTANT?

web-services - 将参数传递给 Web 服务器的两种方法之间的区别?

php - 将身份验证凭据作为 GET 参数发送时的安全问题

php - IIS 7.5 丢失 $_POST 数据?

python - Tornado 占用全部 CPU 并且并发是 Not Acceptable

html - Golang、GAE、重定向用户?

go - 使用 apimachinery/client-go 检索不包含标签的 kubernetes 节点