Golang Go语言中 新年快乐,tostruct 小库来拜年
2023
祝大家新年快乐,新的一年经济恢复,rmb 多多。
github 地址
https://github.com/antlabs/tostruct
tostruct
json/yaml/http header/query string 转成 struct 定义,免去手写 struct 的烦恼.
一、json 字符串生成结构体
import (
"github.com/antlabs/tostruct/json"
"github.com/antlabs/tostruct/option"
)
func main() {
var str = { "action": "Deactivate user", "entities": [ { "uuid": "4759aa70-XXXX-XXXX-925f-6fa0510823ba", "type": "user", "created": 1542595573399, "modified": 1542597578147, "username": "user1", "activated": false, "nickname": "user" }], "timestamp": 1542602157258, "duration": 12 }
// 父子结构合在一起
all, _ := json.Marshal([]byte(str), option.WithStructName("reqName"))
fmt.Println(string(all))
/*
type reqName struct {
Action string `json:"action"`
Duration int `json:"duration"`
Entities []struct {
Activated bool `json:"activated"`
Created int `json:"created"`
Modified int `json:"modified"`
Nickname string `json:"nickname"`
Type string `json:"type"`
Username string `json:"username"`
UUID string `json:"uuid"`
} `json:"entities"`
Timestamp int `json:"timestamp"`
}
*/
// 子结构拆分
all, _ := json.Marshal([]byte(str), option.WithStructName("reqName"), option.WithNotInline())
fmt.Println(string(all))
/*
type reqName struct {
Action string `json:"action"`
Duration int `json:"duration"`
Entities []Entities `json:"entities"`
Timestamp int `json:"timestamp"`
}
type Entities struct {
Activated bool `json:"activated"`
Created int `json:"created"`
Modified int `json:"modified"`
Nickname string `json:"nickname"`
Type string `json:"type"`
Username string `json:"username"`
UUID string `json:"uuid"`
}
*/
}
二、http header 生成结构体
import (
"github.com/antlabs/tostruct/header"
"github.com/antlabs/tostruct/option"
"net/http"
)
func main() {
h := http.header{
“bool”: []string{“true”},
“int” : []string{“1”},
“string” : []string{“hello”},
“float64” : []string{“3.14”},
}
res, err := header.Marshal(h, option.WithStructName(“test”), option.WithTagName(“header”))
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(res))
/*
type test struct {
Bool bool header:"bool"
Float64 float64 header:"float64"
Int int header:"int"
String string header:"string"
}
*/
}
三、查询字符串生成结构体
import (
"github.com/antlabs/tostruct/url"
"github.com/antlabs/tostruct/option"
)
func main() {
url := “http://127.0.0.1:8080?int=1&float64=1.1&bool=true&string=hello”
res, err := header.Marshal(h, option.WithStructName(“test”), option.WithTagName(“form”))
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(res))
/*
type test struct {
Bool bool form:"bool"
Float64 float64 form:"float64"
Int int form:"int"
String string form:"string"
}
*/
}
四、yaml 生成结构体
import (
"github.com/antlabs/tostruct/url"
"github.com/antlabs/tostruct/option"
"github.com/antlabs/tostruct/yaml"
)
func main() {
str := `
a: 1
b: 3.14
c: hello
d:
-
aa
-
bb
e:
-
a: 1
b: 3.14
c: hello
f:
first: 1
second: 3.14
`
all, err := Marshal([]byte(str), option.WithStructName(“reqName”))
if err != nil {
return
}
fmt.Println(all)
/*
type reqName struct {
A int yaml:"a"
B float64 yaml:"b"
C string yaml:"c"
D []string yaml:"d"
E []struct {
A int yaml:"a"
B float64 yaml:"b"
C string yaml:"c"
} yaml:"e"
F struct {
First int yaml:"first"
Second float64 yaml:"second"
} yaml:"f"
}
*/
all, err := Marshal([]byte(str), option.WithStructName(“reqName”), option.WithNotInline())
if err != nil {
return
}
fmt.Println(all)
/*
type reqName struct {
A int yaml:"a"
B float64 yaml:"b"
C string yaml:"c"
D []string yaml:"d"
E []E yaml:"e"
F F yaml:"f"
}
type E struct {
A int yaml:"a"
B float64 yaml:"b"
C string yaml:"c"
}
type F struct {
First int yaml:"first"
Second float64 yaml:"second"
}
*/
}
Golang Go语言中 新年快乐,tostruct 小库来拜年
更多关于Golang Go语言中 新年快乐,tostruct 小库来拜年的实战教程也可以访问 https://www.itying.com/category-94-b0.html
提个建议哈,可以加上命令行功能,用户可以配合 gogenerate 使用而不需要再写一个程序。
更多关于Golang Go语言中 新年快乐,tostruct 小库来拜年的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
这块已经在做,哈哈。。。代码在私有仓库中,过几天会开放出来。
已 star
建议做成在线网页版
有了,谷歌一下
我可能不做网页版的,会根据 dsl 生成 http client/codemsg/http server 之类的代码(在另一个仓库中)。这块还没有同类库。根据 json 生成结构体大部分还是服务这些需求,何不更进一步,直接生成所要代码。
很棒,已 Star ,期待命令行功能
命令行的功能已放开 https://github.com/antlabs/h2o
很棒,规整了很多模型转换。但是,很多时候生成模型只是为了实现 http client, server, grpc ,何不更进一步,直接生成所需代码。我在 https://github.com/antlabs/h2o 库就在做这样的尝试。曾经 30 多个 http client 写配置一天生成完。可否加个微信,NzEwMzkwNTE1 。一起讨论下代码生成的话题。
新年快乐!很高兴在新的一年里与你探讨Golang(Go语言)的相关话题。你提到的“tostruct”小库,虽然这不是Go语言标准库中的一部分,但听起来像是一个用于处理结构体(struct)相关功能的第三方库。在Go语言中,结构体是一种非常重要的数据类型,它允许我们将多个不同类型的数据组合成一个单一的类型。
如果“tostruct”库是用于简化结构体之间的转换、映射或解析等操作,那么它在处理复杂数据结构时可能会非常有用。例如,在Web开发中,我们经常需要将JSON数据解析为Go语言中的结构体,或者将结构体序列化为JSON格式发送给客户端。这时,一个高效、易用的结构体处理库可以大大提高开发效率。
不过,由于“tostruct”不是Go语言的标准库,建议你在使用前仔细查阅其文档和示例代码,了解其工作原理和使用方法。同时,也要注意库的版本更新和兼容性问题,以确保你的代码能够稳定运行。
此外,Go语言社区中还有许多其他优秀的第三方库和工具,它们可以帮助你更高效地编写Go代码。如果你对某个特定领域或功能有需求,不妨在Go语言的官方包管理工具或社区论坛中搜索一下,看看是否有现成的解决方案。
最后,祝你新的一年里在Go语言的学习和实践中取得更多进步!