{"id":37135829,"url":"https://github.com/izettle/structextract","last_synced_at":"2026-01-14T15:50:20.799Z","repository":{"id":47778823,"uuid":"86802487","full_name":"iZettle/structextract","owner":"iZettle","description":"Take things out of a struct","archived":false,"fork":false,"pushed_at":"2023-10-06T00:08:23.000Z","size":36,"stargazers_count":15,"open_issues_count":6,"forks_count":2,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-08-14T00:25:27.803Z","etag":null,"topics":["extract","go-library","golang","struct"],"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/iZettle.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-31T09:30:24.000Z","updated_at":"2024-10-12T17:43:46.000Z","dependencies_parsed_at":"2024-06-19T00:01:15.976Z","dependency_job_id":"a0549461-eec1-46ff-a74a-4a54c8401e14","html_url":"https://github.com/iZettle/structextract","commit_stats":null,"previous_names":["intelligentpos/structextract"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/iZettle/structextract","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iZettle%2Fstructextract","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iZettle%2Fstructextract/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iZettle%2Fstructextract/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iZettle%2Fstructextract/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iZettle","download_url":"https://codeload.github.com/iZettle/structextract/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iZettle%2Fstructextract/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28425182,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T15:24:48.085Z","status":"ssl_error","status_checked_at":"2026-01-14T15:23:41.940Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["extract","go-library","golang","struct"],"created_at":"2026-01-14T15:50:20.247Z","updated_at":"2026-01-14T15:50:20.788Z","avatar_url":"https://github.com/iZettle.png","language":"Go","readme":"[![Coverage Status](https://coveralls.io/repos/github/iZettle/structextract/badge.svg?branch=master)](https://coveralls.io/github/iZettle/structextract?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/iZettle/structextract)](https://goreportcard.com/report/github.com/iZettle/structextract)\n[![GoDoc](https://godoc.org/github.com/iZettle/structextract?status.svg)](https://godoc.org/github.com/iZettle/structextract)\n\n# structextract\nA very small package that extracts a given struct to an array or to a map.\nThere is option to ignore fields or to use the tag names as key on the struct.\nYou can also add `omitempty` tag option that will ignore empty fields.\n## Install\n\n```bash\ngo get github.com/iZettle/structextract\n```\n\n## Examples \n\n#### Basic Usage\n```go\n       type SampleStruct struct {\n\t\tField1 string `json:\"field1\" db:\"field_1_db\"`\n\t\tField2 string `json:\"field2\" db:\"field_2_db\"`\n\t\tField3 bool   `json:\"field3\" db:\"field_3_db\"`\n\t\tField4 int    `json:\"field4\"`\n\t}\n\t\n\n\tss := SampleStruct{\n\t\tField1: \"value 1\",\n\t\tField2: \"value 2\",\n\t\tField3: true,\n\t\tField4: 123,\n\t}\n\t\n       //Create a new extractor,we have to pass a pointer to a struct\n\textract := New(\u0026ss)\n\t\n\n\t//Values will return the values of every field, []interface\n\t//[\"value 1\", \"value 2\", true, 123]\n\tvalues, _ := extract.Values()\n\t\n\n\t//Names will return a slice of the field names, []string\n\t//[\"Field1\",\"Field2\",\"Field3\",\"Field4\"]\n\tnames, _ := extract.Names()\n\t\n\n\t//NamesFromTag will return a slice of the tag value for the given tag, []string\n\t//[\"field_1_db\",\"field_2_db\",\"field_3_db\"]\n\ttagnames, _ := extract.NamesFromTag(\"db\")\n\n\n        //ValuesFromTag will return a slice of the values of the fields with the give tag\n        //[\"value 1\", \"value 2\", true]\n        valuesFromTag, _ := extract.ValuesFromTag(\"db\")\n\n\n\t//NamesFromTagWithPrefix will return a slice of the tag value for the given tag\n\t//including the provided prefix, []string\n\t//[\"default_field_1_db\",\"default_field_2_db\",\"default_field_3_db\"]\n\ttagwithprefix,_ := extract.NamesFromTagWithPrefix(\"db\",\"default_\")\n\n\n\t//FieldValueMap will return a map of field name to value, map[string]interface{}\n\t//{\"Field1\":\"value 1\",\"Field2\":\"value 2\",\"Field3\":true,\"Field4\":123}\n\tvaluesmap, _ := extract.FieldValueMap()\n\t\n\n\t//FieldValueFromTagMap will return a map of tag value to value, map[string]interface{}\n\t//{\"field1\":\"value 1\",\"field2\":\"value 2\",\"field3\":true,\"field4\":123}\n\ttagmap, _ := extract.FieldValueFromTagMap(\"json\")\n\n        // Mapping between different tags\n\t//{\"field1\":\"field_1_db\",\"field2\":\"field_2_db\",\"field3\":\"field_3_db\"}\n        mapping, _ := extract.TagMapping(\"json\", \"db\")\n\n\t\n```\n#### Ignore Fields\n```go\n       ss := SampleStruct{\n\t\tField1: \"value 1\",\n\t\tField2: \"value 2\",\n\t\tField3: true,\n\t\tField4: 123,\n\t}\n\t\n\n\t// Set all the struct fields that we need to ignore\n\textract := New(\u0026ss).\n\t\tIgnoreField(\"Field2\",\"Field3\")\n\n\t\n\t//The result will be {\"Field1\":\"value 1\",\"Field4\":123},\n\t//all the fields that are ignored are not present.\n\tvaluesmap, _ := extract.FieldValueMap()\n\t\n```\n\n#### Use cases\n\nWe found that is very convenient to use structextract when we want to create sql statements \nor maps with data to update or create.\n\nA sample example that we use the structextract with [Squirrel](https://github.com/Masterminds/squirrel).\n\n```go\n       type SampleStruct struct {\n\t\tField1 string `json:\"field1\" db:\"field_1_db\"`\n\t\tField2 string `json:\"field2\" db:\"field_2_db\"`\n\t\tField3 bool   `json:\"field3\" db:\"field_3_db\"`\n\t\tField4 int    `json:\"field4\"`\n\t}\n\t\n\n\tss := SampleStruct{\n\t\tField1: \"value 1\",\n\t\tField2: \"value 2\",\n\t\tField3: true,\n\t\tField4: 123,\n\t}        \n        \n        //Create a map with all the fields a user can update \n\text := structextract.New(\u0026ss).\n\t\tIgnoreField(\"Field2\")\n\t\t\n    \n\tbm, _ := ext.FieldValueFromTagMap(\"db\")\n\t\n\t//Build the query\n\tquery, args, _ := squirrel.Update(\"DBTable\").\n\t\tSetMap(bm).\n\t\tWhere(squirrel.Eq{\"id\": ss.Field1}).\n\t\tToSql()\n\t\t\n\t//Make the sql request..\t\n```\n\n#### Now with support for embedded structs\n```go\n  type SampleInner struct {\n    Inner string `json:\"inner\"`\n  }\n\n  type SampleOuter struct {\n    SampleInner\n    Field string `json:\"field\"`\n  }\n\n  ss := SampleOuter{SampleInner{\"inner\"}, \"outer\"}\n\n  ext := structextract.New(\u0026ss).UseEmbeddedStructs(true)\n\n  jsonMap, err := ext.FieldValueFromTagMap(\"json\")\n  // jsonMap here would be equal to the following map\n  m := map[string]interface{}{\n    \"field\": \"outer\",\n    \"inner\": \"inner\",\n  }\n```\n\n#### Omit Empty Fields\n```go\n    type SampleStruct struct {\n\t\tField1 string `exampleTag:\"field1\" anotherExampleTag:\"field1,omitempty\"`\n\t\tField2 *int   `exampleTag:\"field2,omitempty\" anotherExampleTag:\"field_2\"`\n\t}\n\t// struct with empty values\n        ss := SampleStruct{}\n        result, _ := New(\u0026ss).\n\t\tFieldValueFromTagMap(\"exampleTag\")\n\t\n        // only fields which tag `exampleTag` and omitempty have been ignored\n        // result: map[string]interface{}{\n        //    \"field1\": \"\",\n        //  }\n\t\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizettle%2Fstructextract","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizettle%2Fstructextract","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizettle%2Fstructextract/lists"}