{"id":28387059,"url":"https://github.com/li-giegie/go-jeans","last_synced_at":"2025-07-04T21:04:28.730Z","repository":{"id":163820398,"uuid":"639244915","full_name":"Li-giegie/go-jeans","owner":"Li-giegie","description":"go-jeans是一个高效的数据序列化与反序列化库，解决手动封装数据结构所带来的繁琐问题，通过将数据结构快速编码为二进制格式，比主流编码格式更优越的性能表现，使得数据的传输和存储更加高效可靠。","archived":false,"fork":false,"pushed_at":"2024-08-22T07:57:59.000Z","size":6452,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-30T21:42:16.239Z","etag":null,"topics":["decode","encode","go","high-performance"],"latest_commit_sha":null,"homepage":"https://www.bilibili.com/video/BV1Xy421a7qc/?share_source=copy_web\u0026vd_source=fc15c02da51da27d5acd2a7e03f1001a","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"wtfpl","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Li-giegie.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-11T04:24:32.000Z","updated_at":"2025-02-19T15:58:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"6e3592a0-b585-4042-a2b3-7af0228eee9c","html_url":"https://github.com/Li-giegie/go-jeans","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/Li-giegie/go-jeans","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Li-giegie%2Fgo-jeans","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Li-giegie%2Fgo-jeans/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Li-giegie%2Fgo-jeans/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Li-giegie%2Fgo-jeans/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Li-giegie","download_url":"https://codeload.github.com/Li-giegie/go-jeans/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Li-giegie%2Fgo-jeans/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262132402,"owners_count":23263990,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decode","encode","go","high-performance"],"created_at":"2025-05-30T16:39:33.017Z","updated_at":"2025-06-26T19:31:45.962Z","avatar_url":"https://github.com/Li-giegie.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-jeans\ngo-jeans是一个高效的数据序列化与反序列化库，解决手动封装数据结构所带来的繁琐问题，通过将数据结构快速编码为二进制格式，比主流编码格式更优越的性能表现，使得数据的传输和存储更加高效可靠。\n### 特性\n- 脱离反射机制\n- 资源消耗小\n- 堪比手动封装\n- 支持Go中的基本数据类型和切片类型\n\n### 下载安装\n``` \ngo get -u github.com/Li-giegie/go-jeans\n```\n\n### 使用方法\n[编解码支持列表](#支持列表)  \n编码方法：  \n基础类型 EncodeBaseFaster(buf []byte, args ...interface{}) ([]byte, error)  \n- buf 为存放结果的缓冲区，编码结果为函数最终返回值并非该参数\n- args 为要编码的字段，字段类型参考支持列表，\n\n基础类型 EncodeBase(args ...interface{}) ([]byte, error)\n- args 为要编码的字段，字段类型参考支持列表，\n\n切片类型 EncodeSlice(slice ...interface{}) ([]byte, error)\n- slice 为要编码的字段，字段类型参考支持列表\n\n切片类型 EncodeSliceFaster(buf []byte, slice ...interface{}) ([]byte, error)\n- buf 为存放结果的缓冲区，编码结果为函数最终返回值并非该参数\n- slice 为要编码的字段，字段类型参考支持列表\n\n混合类型 Encode、EncodeFaster参数为基本类型切片类型\n\n解码方法：\n\n基本类型 DecodeBase(buf []byte, args ...interface{}) error\n- buf 为编码的字节切片\n- args 为要还原的指针参数类型列表\n\n切片类型 DecodeSlice(buf []byte, slice ...interface{}) error\n- buf 为编码的字节切片\n- slice 为要还原的指针切片类型参数列表\n\n混合类型 Decode参数为基本指针类型切片指针类型\n\n```go\n// 编码（序列化） 将go中的基本值类型进行编码，编码的参数和解码的参数顺序必须一致，只有在传递的类型不支持时会返回错误，其他情况不会，注意这一步并不打包返回的切片\nfunc TestEncode(t *testing.T) {\n    var s struct{\n        A int\n        B string\n        C bool\n    }\n\n    buf ,err := Encode(s.A,s.B,s.C)\n    if err != nil {\n        return\n    }\n    fmt.Println(buf)\n}\n\n// 解码 将一个字节切片序列化成入参的值，参数要求是GO的基本类型，指针形式传递，编码的参数和解码的参数顺序必须一致\nfunc TestDecode(t *testing.T) {\n    var s struct{\n        A int\n        B string\n        C bool\n    }\n    buf, _ := Encode(s.A,s.B,s.C)\n    err := Decode(buf,\u0026s.A,\u0026s.B,\u0026s.C)\n    if err != nil {\n        return\n    }\n    fmt.Println(s)\n}\n\n// 快速编码\nfunc TestEncodeFaster(t *testing.T) {\n    type base struct {\n        I  int\n        Ui uint\n        Bo bool\n        B  byte\n        Bs []byte\n        S  string\n    }\n    var encodeBase = new(base)\n    //伪造数据\n    err := faker.FakeData(encodeBase)\n    if err != nil {\n        t.Error(err)\n        return\n    }\n    // 1.创建一个缓冲切片，容量我们要预估一下，尽量不要让内存不够多次分配会占用可观的性能\n    // 结构体中变量的大概占用大小，处了[]byte、string外其他的内存占用是确定的，[]byte、string处了本身的长度外还包活一个长度字段占4个字节，只有知道长度信息才能够还原\n    bufCap := 8 + 8 + 1 + 1 + (4 + 100) + (4 + 100)\n    // 切片的长度一定是0，容量是我们预估的,CountLength()函数可以计算容量\n    buf := make([]byte, 0, bufCap)\n    buf, err = EncodeFaster(buf, encodeBase.I, encodeBase.Ui, encodeBase.B, encodeBase.Bs, encodeBase.Bo, encodeBase.S)\n    if err != nil {\n        t.Error(err)\n        return\n    }\n    // 解码验证\n    decodeBase := new(base)\n    if err = Decode(buf, \u0026decodeBase.I, \u0026decodeBase.Ui, \u0026decodeBase.B, \u0026decodeBase.Bs, \u0026decodeBase.Bo, \u0026decodeBase.S); err != nil {\n        t.Error(err)\n        return\n    }\n    if !reflect.DeepEqual(encodeBase, decodeBase) {\n        t.Error(\"decode fail\")\n        return\n    }\n    fmt.Printf(\"encodeBase: %v \\ndecodeBase: %v\\n\", encodeBase, decodeBase)\n}\n```\n### 注意事项\n编解码支持的类型仅为Go中的基本类型：int、int8 ~ int64、uint、uint8 ~ uint64、bool、string、float32~64、byte、[]byte(比较常用)，后续考录支持更多的类似\n\n[Encode 编码|序列化：](#) 入参必须为Go中的基本类型，如果确认入参全部被支持，可忽略错误，如果出现错误会返回入参的顺序，例如如果s.A不被支持即返回信息中包含index 0\n\n[Decode 解码|反序列化：](#) 入参必须为Go中的\u003cspan style=\"color: pink\"\u003e指针基本类型\u003c/span\u003e，如果不是指针返回错误，编码的顺序和解码的顺序需要保持一致，可参考使用方法中的示例\n\n### TODO\n优化代码\n\n\n### Benchmark测试\n数据来自我的联想笔记本电脑（仅供参考）\n```\ngo test -run=none -cpu 1 -benchmem -bench=Benchmark\ngoos: windows\ngoarch: amd64\npkg: github.com/Li-giegie/go-jeans\ncpu: AMD Ryzen 5 5600H with Radeon Graphics\nBenchmarkEncode                         12100116               101.4 ns/op           176 B/op          1 allocs/op\nBenchmarkDecode                         32333073                36.67 ns/op            0 B/op          0 allocs/op\nBenchmarkEncodeAndDecode                 7679233               149.2 ns/op           128 B/op          1 allocs/op\nBenchmarkEncodeFaster                   20996823                52.05 ns/op            0 B/op          0 allocs/op\nBenchmarkJsonMarshal                     1348072               994.1 ns/op           448 B/op          2 allocs/op\nBenchmarkJsonUnmarshal                    287700              5054 ns/op             408 B/op         18 allocs/op\nBenchmarkJsonMarshalAndUnmarshal          236295              5114 ns/op             888 B/op         21 allocs/op\nBenchmarkProtoBufMarshal                 5213646               238.8 ns/op            80 B/op          1 allocs/op\nBenchmarkProtoBufUnmarshal               4922341               247.2 ns/op            80 B/op          2 allocs/op\nBenchmarkProtoBufMarshalAndUnmarshal     1991695               579.8 ns/op           384 B/op          4 allocs/op\nBenchmarkMsgPackMarshal                  1504008               803.2 ns/op           496 B/op          4 allocs/op\nBenchmarkMsgPackUnmarshal                1000000              1188 ns/op              80 B/op          2 allocs/op\nBenchmarkGobEncode                       2202207               459.9 ns/op           304 B/op          0 allocs/op\nBenchmarkGobDecode                         52582             22945 ns/op            8672 B/op        253 allocs/op\n```\n\n### 支持列表\n编码支持字段类型：\n\nEncodeBase、EncodeBaseFaster\n- string, int8, uint8, bool, int16, uint16, int32, uint32, float32, int, uint, int64, uint64, float64\n\nEncodeSlice、EncodeSliceFaster\n- []uint, []uint8, []uint16, []uint32, []uint64, []int, []int8, []int16, []int32, []int64, []float32, []float64, []bool, []string\n\nEncode、EncodeFaster 为上面两种编码的混合版，参数只要是上面两种类型的都支持\n\n解码支持字段类型为编码类型的指针：\n\nDecodeBase\n- *string, *int8, *uint8, *bool, *int16, *uint16, *int32, *uint32, *float32, *int, *uint, *int64, *uint64, *float64\n\nDecodeSlice\n- *[]uint, *[]uint8, *[]uint16, *[]uint32, *[]uint64, *[]int, *[]int8, *[]int16, *[]int32, *[]int64, *[]float32, *[]float64, *[]bool, *[]string\n\nDecode 为上面两种解码的混合版","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fli-giegie%2Fgo-jeans","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fli-giegie%2Fgo-jeans","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fli-giegie%2Fgo-jeans/lists"}