{"id":48625549,"url":"https://github.com/tiendc/go-rflutil","last_synced_at":"2026-04-09T04:32:47.798Z","repository":{"id":193911933,"uuid":"670959700","full_name":"tiendc/go-rflutil","owner":"tiendc","description":"Reflection utility functions for Go","archived":false,"fork":false,"pushed_at":"2024-09-19T18:45:12.000Z","size":53,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-06T07:37:42.751Z","etag":null,"topics":["golang","reflect","reflection","struct"],"latest_commit_sha":null,"homepage":"","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/tiendc.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-07-26T08:15:54.000Z","updated_at":"2024-11-18T16:16:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"8cee7c6b-3f5e-4e11-a7ea-f7988d3fb246","html_url":"https://github.com/tiendc/go-rflutil","commit_stats":null,"previous_names":["tiendc/go-rflutil"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tiendc/go-rflutil","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-rflutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-rflutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-rflutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-rflutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiendc","download_url":"https://codeload.github.com/tiendc/go-rflutil/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fgo-rflutil/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31586403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"online","status_checked_at":"2026-04-09T02:00:06.848Z","response_time":112,"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":["golang","reflect","reflection","struct"],"created_at":"2026-04-09T04:32:45.306Z","updated_at":"2026-04-09T04:32:47.781Z","avatar_url":"https://github.com/tiendc.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Go Version][gover-img]][gover] [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![GoReport][rpt-img]][rpt]\n\n# Reflection utility functions for Go\n\n## Installation\n\n```shell\ngo get github.com/tiendc/go-rflutil\n```\n\n## Usage\n\n- [Slice functions](#slice-functions)\n- [Map functions](#map-functions)\n- [Struct functions](#struct-functions)\n- [Common functions](#common-functions)\n\n### Slice functions\n\n#### SliceLen\n\n```go\nslice := []int{1, 2, 3}\nv, err := SliceLen(reflect.ValueOf(slice)) // v == 3\n```\n\n#### SliceGet\n\n```go\nslice := []int{1, 2, 3}\nv, err := SliceGet[int](reflect.ValueOf(slice), 1)     // v == 2\nv, err := SliceGet[int](reflect.ValueOf(slice), 3)     // err is ErrIndexOutOfRange\nv, err := SliceGet[string](reflect.ValueOf(slice), 1)  // err is ErrTypeUnmatched\n```\n\n#### SliceSet\n\n```go\nslice := []int{1, 2, 3}\nerr := SliceSet(reflect.ValueOf(slice), 1, 22)    // slice[1] == 22\nerr := SliceSet(reflect.ValueOf(slice), 3, 44)    // err is ErrIndexOutOfRange\nerr := SliceSet(reflect.ValueOf(slice), 1, \"22\")  // err is ErrTypeUnmatched\n```\n\n#### SliceAppend\n\n```go\nslice := []int{1, 2, 3}\nslice2, err := SliceAppend(reflect.ValueOf(slice), 4)   // slice2 == []int{1, 2, 3, 4}\nslice2, err := SliceAppend(reflect.ValueOf(slice), \"4\") // err is ErrTypeUnmatched\n```\n\n#### SliceGetAll\n\n```go\nslice := []int{1, 2, 3}\ns, err := SliceGetAll(reflect.ValueOf(slice)) // returns []reflect.Value\n```\n\n#### SliceAs\n\n```go\nslice := []any{1, 2, 3}\ns, err := SliceAs[int64](reflect.ValueOf(slice)) // s == []int64{1,2,3}\n```\n\n### Map functions\n\n#### MapLen\n\n```go\naMap := map[int]string{1: \"11\", 2: \"22\", 3: \"33\"}\nv, err := MapLen(reflect.ValueOf(aMap), 1) // v == 3\n```\n\n#### MapGet\n\n```go\naMap := map[int]string{1: \"11\", 2: \"22\", 3: \"33\"}\nv, err := MapGet[string](reflect.ValueOf(aMap), 1) // v == \"11\"\nv, err := MapGet[int](reflect.ValueOf(aMap), 3)    // err is ErrTypeUnmatched\n```\n\n#### MapSet\n\n```go\naMap := map[int]string{1: \"11\", 2: \"22\", 3: \"33\"}\nerr := MapSet(reflect.ValueOf(aMap), 1, \"111\") // success\nerr := MapSet(reflect.ValueOf(aMap), 4, \"444\") // success\nerr := MapSet(reflect.ValueOf(aMap), 5, 555)   // err is ErrTypeUnmatched\n```\n\n#### MapDelete\n\n```go\naMap := map[int]string{1: \"11\", 2: \"22\", 3: \"33\"}\nerr := MapDelete(reflect.ValueOf(aMap), 1) // success\nerr := MapDelete(reflect.ValueOf(aMap), 4) // success\n```\n\n#### MapKeys\n\n```go\naMap := map[int]string{1: \"11\", 2: \"22\", 3: \"33\"}\nkeys, err := MapKeys(reflect.ValueOf(aMap)) // returns a slice []reflect.Value of keys\n```\n\n#### MapEntries\n\n```go\naMap := map[int]string{1: \"11\", 2: \"22\", 3: \"33\"}\nentries, err := MapEntries(reflect.ValueOf(aMap)) // returns a slice []MapEntry of entries\n```\n\n### Struct functions\n\n#### StructGetField\n\n```go\ntype Struct struct {\n    I int\n    S string\n}\ns := Struct{I: 1, S: \"11\"}\nv, err := StructGetField[int](reflect.ValueOf(s), \"I\", true)     // v == 1\nv, err := StructGetField[string](reflect.ValueOf(s), \"S\", true)  // v == \"11\"\nv, err := StructGetField[string](reflect.ValueOf(s), \"s\", false) // v == \"11\"\nv, err := StructGetField[string](reflect.ValueOf(s), \"s\", true)  // err is ErrNotFound\n```\n\n#### StructSetField\n\n```go\ntype Struct struct {\n    I int\n    S string\n}\ns := Struct{I: 1, S: \"11\"}\nerr := StructSetField[int](reflect.ValueOf(\u0026s), \"I\", 11, true)        // success\nerr := StructSetField[string](reflect.ValueOf(\u0026s), \"S\", \"111\", true)  // success\nerr := StructSetField[string](reflect.ValueOf(\u0026s), \"s\", \"111\", false) // success\nerr := StructSetField[string](reflect.ValueOf(\u0026s), \"s\", \"111\", true)  // err is ErrNotFound\n```\n\n#### StructListFields\n\n```go\ntype Base struct {\n    I  int\n    S2 string\n}\ntype Struct struct {\n    Base\n    I int\n    S string\n}\ns := Struct{}\nfields, err := StructListFields(reflect.ValueOf(\u0026s), false) // returns []string{\"Base\", \"I\", \"S\"}\nfields, err := StructListFields(reflect.ValueOf(\u0026s), true)  // returns []string{\"S2\", \"I\", \"S\"}\n```\n\n#### StructToMap\n\n```go\ntype Base struct {\n    I  int     `json:\"i\"`\n    S2 string  `json:\"s2\"`\n}\ntype Struct struct {\n    Base\n    I int    `json:\"i\"`\n    S string `json:\"s\"`\n}\n\ns := Struct{I: 1, S: \"S\", Base: Base{I: 2, S2: \"S2\"}}\n\n// Converts without flattening the embedded struct\nm, err := StructToMap(reflect.ValueOf(\u0026s), \"\", false)    // m == map[string]any{\"Base\": Base{I: 2, S2: \"S2\"}, \"I\": 1, \"S\": \"S\"}\n\n// Converts with flattening the embedded struct\nm, err := StructToMap(reflect.ValueOf(\u0026s), \"\", true)     // m == map[string]any{\"S2\": \"S2\", \"I\": 1, \"S\": \"S\"}\n\n// Converts with parsing json tag\nm, err := StructToMap(reflect.ValueOf(\u0026s), \"json\", true) // m == map[string]any{\"s2\": \"S2\", \"i\": 1, \"s\": \"S\"}\n```\n\n#### ParseTag / ParseTagOf / ParseTagsOf\n\n```go\ntype S struct {\n    I int    `mytag:\"i,optional,k=v\"`\n    S string `mytag:\"s,optional,k1=v1,k2=v2,omitempty\"`\n    U uint   `mytag:\"-,optional\"`\n}\n\ns := S{I: 1, S: \"11\", U: 10}\nsVal := reflect.ValueOf(s)\niField, _ := sVal.Type().FieldByName(\"I\")\ntag, err := ParseTag(\u0026iField, \"mytag\", \",\") // tag.Name == i\n                                            // tag.Attrs == map[string]string{\"optional\": \"\", \"k\", \"v\"}\n```\n\n### Common functions\n\n#### ValueAs\n\n```go\nv, err := ValueAs[float32](reflect.ValueOf(97)) // v == float32(97)\nv, err := ValueAs[string](reflect.ValueOf(97))  // v == \"a\"\n```\n\n## Contributing\n\n- You are welcome to make pull requests for new functions and bug fixes.\n\n## License\n\n- [MIT License](LICENSE)\n\n[doc-img]: https://pkg.go.dev/badge/github.com/tiendc/go-rflutil\n[doc]: https://pkg.go.dev/github.com/tiendc/go-rflutil\n[gover-img]: https://img.shields.io/badge/Go-%3E%3D%201.18-blue\n[gover]: https://img.shields.io/badge/Go-%3E%3D%201.18-blue\n[ci-img]: https://github.com/tiendc/go-rflutil/actions/workflows/go.yml/badge.svg\n[ci]: https://github.com/tiendc/go-rflutil/actions/workflows/go.yml\n[cov-img]: https://codecov.io/gh/tiendc/go-rflutil/branch/main/graph/badge.svg\n[cov]: https://codecov.io/gh/tiendc/go-rflutil\n[rpt-img]: https://goreportcard.com/badge/github.com/tiendc/go-rflutil\n[rpt]: https://goreportcard.com/report/github.com/tiendc/go-rflutil\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiendc%2Fgo-rflutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiendc%2Fgo-rflutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiendc%2Fgo-rflutil/lists"}