{"id":13412896,"url":"https://github.com/tomwright/queryparam","last_synced_at":"2025-06-28T04:36:33.361Z","repository":{"id":57502451,"uuid":"137342838","full_name":"TomWright/queryparam","owner":"TomWright","description":"Go package to easily convert a URL's query parameters/values into usable struct values of the correct types.","archived":false,"fork":false,"pushed_at":"2020-09-23T15:23:11.000Z","size":40,"stargazers_count":19,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-05T23:17:35.702Z","etag":null,"topics":["delimited","delimited-data","go","golang","query-parameters","query-params","slice","tag","tags","url","url-parameters","url-params","url-parser","url-parsing","url-values"],"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/TomWright.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":"2018-06-14T10:23:05.000Z","updated_at":"2023-10-29T08:49:21.000Z","dependencies_parsed_at":"2022-09-13T07:02:24.208Z","dependency_job_id":null,"html_url":"https://github.com/TomWright/queryparam","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/TomWright/queryparam","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fqueryparam","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fqueryparam/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fqueryparam/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fqueryparam/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomWright","download_url":"https://codeload.github.com/TomWright/queryparam/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fqueryparam/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262376236,"owners_count":23301345,"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":["delimited","delimited-data","go","golang","query-parameters","query-params","slice","tag","tags","url","url-parameters","url-params","url-parser","url-parsing","url-values"],"created_at":"2024-07-30T20:01:30.773Z","updated_at":"2025-06-28T04:36:33.344Z","avatar_url":"https://github.com/TomWright.png","language":"Go","readme":"# Query Param\n\n[![Build Status](https://travis-ci.org/TomWright/queryparam.svg?branch=master)](https://travis-ci.org/TomWright/queryparam)\n[![codecov](https://codecov.io/gh/TomWright/queryparam/branch/master/graph/badge.svg)](https://codecov.io/gh/TomWright/queryparam)\n[![Go Report Card](https://goreportcard.com/badge/github.com/TomWright/queryparam)](https://goreportcard.com/report/github.com/TomWright/queryparam)\n[![Documentation](https://godoc.org/github.com/TomWright/queryparam?status.svg)](https://godoc.org/github.com/TomWright/queryparam)\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n\nStop accessing query strings and repeatedly parsing them into your preferred values - `queryparam` can do that for you! \n\n## Installation\n\n```\ngo get -u github.com/tomwright/queryparam\n```\n\n## Usage\n\nPlease use the latest major version. This requires the `/v4` at the end of the import as per the go mod documentation.\n\n```\nimport github.com/tomwright/queryparam/v4\n```\n\n## Examples\n\nFor examples see [godoc examples](https://godoc.org/github.com/TomWright/queryparam#example-Parse).\n\n## Quickstart\n\nTransform your http handlers from this...\n```\nfunc searchUsersHandler(r *http.Request, rw http.ResponseWriter) {\n\tvalues := r.URL.Query()\n\n\tuserIDs := make([]string, 0)\n\tif userIDsStr := values.Get(\"id\"); userIDsStr != \"\" {\n\t\tuserIDs = strings.Split(userIDsStr, \",\")\n\t}\n\tteamIDs := make([]string, 0)\n\tif teamIDsStr := values.Get(\"team-id\"); teamIDsStr != \"\" {\n\t\tteamIDs = strings.Split(teamIDsStr, \",\")\n\t}\n\tmustBeActive := false\n\tswitch strings.ToLower(values.Get(\"must-be-active\")) {\n\tcase \"true\", \"yes\", \"y\":\n\t\tmustBeActive = true\n\tcase \"\":\n\t\tbreak\n\tdefault:\n\t\t// unhandled bool value... handle as 400 or ignore to default as false\n\t}\n\tcreatedAfter := time.Time{}\n\tif createdAfterStr := values.Get(\"must-be-active\"); createdAfterStr != \"\" {\n\t\tvar err error\n\t\tcreatedAfter, err = time.Parse(time.RFC3339, createdAfterStr)\n\t\tif err != nil {\n\t\t\t// bad time value\n\t\t}\n\t}\n\n\tusers, err := searchUsers(userIDs, teamIDs, mustBeActive, createdAfter)\n\n\t// handle users and err...\n}\n```\n\nTo this...\n```\nfunc searchUsersHandler(r *http.Request, rw http.ResponseWriter) {\n\treq := struct {\n\t\tUserIDs      []string  `queryparam:\"id\"`\n\t\tTeamIDs      []string  `queryparam:\"team-id\"`\n\t\tMustBeActive bool      `queryparam:\"must-be-active\"`\n\t\tCreatedAfter time.Time `queryparam:\"created-after\"`\n\t}{}\n\n\terr := queryparam.Parse(r.URL.Query(), \u0026req)\n\tswitch err {\n\tcase nil:\n\t\tbreak\n\tcase queryparam.ErrInvalidBoolValue: // only necessary if the request contains a bool value\n\t\t// they have entered a non-bool value.\n\t\t// this can be handled this as a 400 or ignored to default to false.\n\t\treturn\n\tdefault:\n\t\t// something went wrong when parsing a value.\n\t\t// send a 500.\n\t\treturn\n\t}\n\n\tusers, err := searchUsers(req.UserIDs, req.TeamIDs, req.MustBeActive, req.CreatedAfter)\n\n\t// handle users and err...\n}\n```\n\n## Types\n\nBy default `queryparam` can parse the following types.\n\n- `string`\n- `[]string`\n- `int`\n- `int32`\n- `int64`\n- `float32`\n- `float64`\n- `bool`\n- `time.Time`\n- `queryparam.Present`\n\n### Custom Types\n\nYou can add custom type parsers and setters with the following:\n\n```\n// your custom type.\ntype MyCustomStringType string\n\n// add a value parser for the custom type.\nqueryparam.DefaultParser.ValueParsers[reflect.TypeOf(MyCustomStringType(\"\"))] = func(value string, _ string) (reflect.Value, error) {\n    return reflect.ValueOf(MyCustomStringType(value)), nil\n}\n```\n\nYou can override the default value parsers in a similar manner...\n```\nqueryparam.DefaultParser.ValueParsers[reflect.TypeOf(\"\")] = func(value string, _ string) (reflect.Value, error) {\n    // my custom string parser\n    return reflect.ValueOf(value), nil\n}\n```\n","funding_links":[],"categories":["Forms","表单","表单`表单解析与绑定`"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","SQL 查询语句构建库","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwright%2Fqueryparam","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomwright%2Fqueryparam","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwright%2Fqueryparam/lists"}