{"id":13764076,"url":"https://github.com/PumpkinSeed/structs","last_synced_at":"2025-05-10T17:31:34.296Z","repository":{"id":57496978,"uuid":"101477714","full_name":"PumpkinSeed/structs","owner":"PumpkinSeed","description":"Golang struct operations.","archived":false,"fork":false,"pushed_at":"2017-10-23T13:03:17.000Z","size":28,"stargazers_count":24,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-06T13:17:01.562Z","etag":null,"topics":["golang","golang-library","golang-struct","library","reflection","utility"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PumpkinSeed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-26T09:59:00.000Z","updated_at":"2023-12-29T08:01:20.000Z","dependencies_parsed_at":"2022-09-03T02:00:13.058Z","dependency_job_id":null,"html_url":"https://github.com/PumpkinSeed/structs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PumpkinSeed%2Fstructs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PumpkinSeed%2Fstructs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PumpkinSeed%2Fstructs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PumpkinSeed%2Fstructs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PumpkinSeed","download_url":"https://codeload.github.com/PumpkinSeed/structs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253453329,"owners_count":21911079,"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":["golang","golang-library","golang-struct","library","reflection","utility"],"created_at":"2024-08-03T15:01:12.220Z","updated_at":"2025-05-10T17:31:33.959Z","avatar_url":"https://github.com/PumpkinSeed.png","language":"Go","readme":"# Golang structs\nPackage structs implements simple functions to manipulate structs in Golang.\n\n[![Documentation](https://godoc.org/github.com/PumpkinSeed/structs?status.svg)](https://godoc.org/github.com/PumpkinSeed/structs) \n[![Go Report Card](https://goreportcard.com/badge/github.com/PumpkinSeed/structs)](https://goreportcard.com/report/github.com/PumpkinSeed/structs)\n[![license](https://img.shields.io/github/license/yangwenmai/how-to-add-badge-in-github-readme.svg?maxAge=2592000)](github.com/PumpkinSeed/structs/LICENSE.md)\n[![Build Status](https://travis-ci.org/PumpkinSeed/structs.svg?branch=master)](https://travis-ci.org/PumpkinSeed/structs)\n[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go#utilities)\n\n\n## Get it\n\n```\ngo get github.com/PumpkinSeed/structs\n```\n\n## Contains\nContains reports whether value is within struct\n\n```\npackage main\n\nimport \"github.com/PumpkinSeed/structs\"\n\ntype Tst struct {\n    TestString  string\n    TestFloat32 float32\n    TestFloat64 float64\n}\n\nfunc main() {\n    tst := Tst{\n        TestString:  \"test\",\n        TestFloat32: 13.444,\n        TestFloat64: 16.444,\n    }\n\n    result := structs.Contains(tst, float64(16.444)) // true\n    result = structs.Contains(tst, float32(13.444)) // true\n}\n```\n\n#### Benchmark\n\n```\nBenchmarkContains-4   \t 3000000\t       492 ns/op\n```\n\n## Compare\nCompare returns a boolean comparing two struct\n\n```\npackage main\n\nimport \"github.com/PumpkinSeed/structs\"\n\ntype TstA struct {\n\tTestInt   int\n\tTestInt8  int8\n\tTestInt16 int16\n}\n\ntype TstB struct {\n\tTestInt   int\n\tTestInt8  int8\n\tTestInt16 int16\n}\n\nfunc main() {\n    tstA := TstA{\n        TestInt:   12,\n\t    TestInt8:  42,\n\t    TestInt16: 55,\n    }\n\n    tstB := TstB{\n        TestInt:   12,\n\t    TestInt8:  42,\n\t    TestInt16: 55,\n    }\n\n    result := structs.Compare(testStructA, testStructB) // true\n}\n\n\n```\n\n#### Benchmark\n\n```\nBenchmarkCompareEqual-4      \t 5000000\t       379 ns/op\nBenchmarkCompareNotEqual-4   \t 5000000\t       372 ns/op\n```\n\n## Index\nIndex returns the index of the first instance of the value in struct\n\n```\npackage main\n\nimport \"github.com/PumpkinSeed/structs\"\n\ntype Tst struct {\n    TestInt     int\n\tTestInt8    int8\n\tTestInt16   int16\n\tTestInt32   int32\n\tTestInt64   int64\n\tTestString  string\n\tTestBool    bool\n\tTestFloat32 float32\n\tTestFloat64 float64\n}\n\nfunc main() {\n    tst := Tst{\n        TestInt:     12,\n\t\tTestInt8:    42,\n\t\tTestInt16:   55,\n\t\tTestInt32:   33,\n\t\tTestInt64:   78,\n\t\tTestString:  \"test\",\n\t\tTestBool:    false,\n\t\tTestFloat32: 13.444,\n\t\tTestFloat64: 16.444,\n    }\n\n    result := structs.Index(testStruct, \"test\") // 5\n}\n```\n\n#### Benchmark\n\n```\nBenchmarkIndex-4             \t 5000000\t       242 ns/op\n```\n\n## FieldNameByValue\nFieldNameByValue returns the field's name of the first instance of the value in struct\n\n```\npackage main\n\nimport \"github.com/PumpkinSeed/structs\"\n\ntype Tst struct {\n    TestInt     int\n\tTestInt8    int8\n\tTestInt16   int16\n\tTestInt32   int32\n\tTestInt64   int64\n\tTestString  string\n\tTestBool    bool\n\tTestFloat32 float32\n\tTestFloat64 float64\n}\n\nfunc main() {\n    tst := Tst{\n        TestInt:     12,\n\t\tTestInt8:    42,\n\t\tTestInt16:   55,\n\t\tTestInt32:   33,\n\t\tTestInt64:   78,\n\t\tTestString:  \"test\",\n\t\tTestBool:    false,\n\t\tTestFloat32: 13.444,\n\t\tTestFloat64: 16.444,\n    }\n\n    result := structs.FieldNameByValue(testStruct, \"test\") // TestString\n}\n```\n\n#### Benchmark\n\n```\nBenchmarkFieldNameByValue-4   \t 5000000\t       293 ns/op\n```\n\n## Map\nThe second parameter is a function, apply the function on each field on the struct, or on the condition determined in the third argument\n\n```\npackage main\n\nimport \"github.com/PumpkinSeed/structs\"\n\ntype Tst struct {\n\tUsername string\n\tTitle    string\n\tContent  string\n}\n\nfunc main() {\n    tst := Tst{\n\t\tUsername: \"PumpkinSeed\",\n\t\tTitle:    \"Test title\",\n\t\tContent:  \"Test content\",\n\t}\n\n    result, err := structs.Map(\u0026ts, func(v reflect.Value) error {\n\t\tif v.Type() == stringType {\n\t\t\tv.SetString(strings.ToLower(v.String()))\n\t\t}\n\t\treturn nil\n\t})\n}\n```\n\n#### Benchmark\n\n```\nBenchmarkMap-4                \t 5000000\t       268 ns/op\n```\n\n## Replace\nReplace returns a copy of the struct with the first non-overlapping instance of old replaced by new, the last param (n) is the limit, if n \u003c 0, there is no limit on the number of replacements\n\n```\npackage main\n\nimport \"github.com/PumpkinSeed/structs\"\n\ntype Tst struct {\n    TestInt     int\n\tTestInt8    int8\n\tTestInt16   int16\n\tTestInt32   int32\n\tTestInt64   int64\n\tTestString1    string\n\tTestString2    string\n\tTestString3    string\n\tTestString4    string\n\tTestBool    bool\n\tTestFloat32 float32\n\tTestFloat64 float64\n}\n\nfunc main() {\n    tst := Tst{\n        TestInt:     12,\n\t\tTestInt8:    42,\n\t\tTestInt16:   55,\n\t\tTestInt32:   33,\n\t\tTestInt64:   78,\n\t\tTestString1:    \"test\",\n\t\tTestString2:    \"test\",\n\t\tTestString3:    \"test\",\n\t\tTestString4:    \"test\",\n\t\tTestBool:    false,\n\t\tTestFloat32: 13.444,\n\t\tTestFloat64: 16.444,\n    }\n\n    result, err := structs.Replace(\u0026ts, \"test\", \"new\", 2)\n}\n```\n\n#### Benchmark\n\n```\nBenchmarkReplace-4            \t 2000000\t       655 ns/op\n```\n\n### ToDo\n- Upgrade GoDoc\n- Implement Map\n","funding_links":[],"categories":["实用工具","Utilities","公用事业公司","工具库","工具库`可以提升效率的通用代码库和工具`","Utility"],"sub_categories":["Advanced Console UIs","Utility/Miscellaneous","HTTP Clients","实用程序/Miscellaneous","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","查询语","交流","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPumpkinSeed%2Fstructs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPumpkinSeed%2Fstructs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPumpkinSeed%2Fstructs/lists"}