{"id":22993891,"url":"https://github.com/erizocosmico/bindec","last_synced_at":"2025-09-13T10:50:42.394Z","repository":{"id":57694446,"uuid":"173768437","full_name":"erizocosmico/bindec","owner":"erizocosmico","description":"bindec generates encoders and decoders to encode and decode the binary representation of types","archived":false,"fork":false,"pushed_at":"2019-04-29T22:48:30.000Z","size":66,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T12:37:45.058Z","etag":null,"topics":["binary","codegen","decoder","encoder","golang","serialization"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erizocosmico.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}},"created_at":"2019-03-04T15:15:01.000Z","updated_at":"2024-10-25T11:33:19.000Z","dependencies_parsed_at":"2022-09-26T16:40:16.496Z","dependency_job_id":null,"html_url":"https://github.com/erizocosmico/bindec","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/erizocosmico/bindec","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erizocosmico%2Fbindec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erizocosmico%2Fbindec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erizocosmico%2Fbindec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erizocosmico%2Fbindec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erizocosmico","download_url":"https://codeload.github.com/erizocosmico/bindec/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erizocosmico%2Fbindec/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274952727,"owners_count":25380233,"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","status":"online","status_checked_at":"2025-09-13T02:00:10.085Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["binary","codegen","decoder","encoder","golang","serialization"],"created_at":"2024-12-15T05:15:30.189Z","updated_at":"2025-09-13T10:50:42.376Z","avatar_url":"https://github.com/erizocosmico.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bindec [![Build Status](https://travis-ci.org/erizocosmico/bindec.svg?branch=master)](https://travis-ci.org/erizocosmico/bindec) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Go Report Card](https://goreportcard.com/badge/github.com/erizocosmico/bindec)](https://goreportcard.com/report/github.com/erizocosmico/bindec)\n\n`bindec` generates encoders and decoders to encode and decode binary representations of types. Encoders and decoders are code-generated, thus generating code tailored specifically for your type, making encoding and decoding really fast.\n\n### Install\n\n```\ngo get github.com/erizocosmico/bindec/cmd/...\n```\n\n### Usage\n\n```\nbindec -type=MyType\n```\n\nSince the default receiver is `t` and that might cause a lint error if your type has other methods with a different receiver, you can use the `-recv` argument.\n\n```\nbindec -recv=mt -type=MyType\n```\n\nYou can also generate a encoders and decoders for a type from a package that is not your working directory by simply passing the path as an argument after all the other flags.\n\n```\nbindec -recv=somerecv -type=SomeType /path/to/package\n```\n\nIf you want to generate encoders and decoders for more than one file and output them in the same file, you can do so passing comma-separated types.\n\n```\nbindec -type=FirstType,SecondType,ThirdType\n```\n\nIf you give them receiver names, you must give them in the same order as the types are passed, also comma-separated.\n\n```\nbindec -recv=a,b,c -type=A,B,C\n```\n\n### Encode and decode\n\nAfter generating the code you will have in your package a file `yourtype_bindec.go` with four methods added to the type: `EncodeBinary`, `WriteBinary`, `DecodeBinaryFromBytes` and `DecodeBinary`.\n\n```go\n// This is our type.\ntype Person {\n    Name string\n    Age int\n    Gender string\n}\n\n// EncodeBinary and DecodeBinary will be on person_bindec.go\n\n// We get some data from somewhere as []byte\ndata := getSomeDataFromSomewhere()\n\nvar p Person\n// Then we decode the person from the data.\nif err := p.DecodeBinaryFromBytes(data); err != nil {\n    // handle err\n}\n\n// We can even read it from a reader.\nvar p2 Person\nif err := p.DecodeBinary(reader); err != nil {\n    // handle err\n}\n\n// And encode it again.\nencoded, err := p.EncodeBinary()\nif err != nil {\n    // handle err\n}\n\n// Or write it to a writer.\nwriter := bytes.NewBuffer(nil)\nif err := p.WriteBinary(writer); err != nil {\n    // handle err\n}\n```\n\n### Ignore fields\n\nYou may have fields you don't want to encode or decode. You can do so using `bindec:\"-\"` struct tag.\n\n```go\ntype User struct {\n    Username string\n    Email string\n    Password string `bindec:\"-\"`\n}\n```\n\nThis way, only `Username` and `Email` will be encoded and decoded, but not `Password`.\n\n### Benchmarks\n\nSpeed:\n\n```\ngoos: darwin\ngoarch: amd64\npkg: github.com/erizocosmico/bindec/bench\nBenchmarkEncode/bindec-4                  500000              2274 ns/op     576 B/op           6 allocs/op\nBenchmarkEncode/gob-4                     100000             12464 ns/op    2432 B/op          41 allocs/op\nBenchmarkDecode/bindec-4                  500000              2590 ns/op     528 B/op          22 allocs/op\nBenchmarkDecode/gob-4                      50000             37126 ns/op    9572 B/op         249 allocs/op\nPASS\nok      github.com/erizocosmico/bindec/bench    6.137s\n```\n\nSize:\n\n```\nBINDEC: 112 bytes\nGOB: 205 bytes\n```\n\nBenchmarked against `encoding/gob` on a MacBook Pro (Retina, 13-inch, Early 2015) (2,7 GHz Intel Core i5) on macOS Mojave 10.14.3.\n\n### Why not just `encoding/gob`?\n\nIt's slower, takes more space and requires registering every single type that's going to be encoded.\n`bindec` offers a fast way to convert a struct into bytes with the smallest possible size. All you need to do is add `//go:generate` tags to you code and run `go generate ./...`.\n\nIn a future version, `bindec` will support adding validations to the fields during decoding via struct tags, which will allow things like \"accept only 100 bytes on this field so an atacker can't send as many bytes as they want\" to be performed automatically.\n\n### Supported types\n\n- Integers: byte, int, uint, uint64, ...\n- Floats: float32, float64\n- Strings\n- Maps with keys and values of supported types\n- Pointers\n- Structs (without cyclic references) with fields of supported types\n- Arrays of supported types\n- Slices of supported types\n- Booleans\n\n### Limitations\n\n- Interface, function and channel types are not supported. The reason interfaces are not supported is because they can be anything, potentially even from any package, on runtime and bindec decoders and encoders are generated beforehand.\n- Cyclic structures are not supported.\n\n### Specification\n\nFor more details about the format used to encode the types, see [SPEC.md](/SPEC.md).\n\n### Constraints\n\nTODO.\n\n### LICENSE\n\nMIT License, see [LICENSE](/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferizocosmico%2Fbindec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferizocosmico%2Fbindec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferizocosmico%2Fbindec/lists"}