{"id":13471468,"url":"https://github.com/guregu/null","last_synced_at":"2025-05-14T00:03:07.091Z","repository":{"id":20161816,"uuid":"23432446","full_name":"guregu/null","owner":"guregu","description":"reasonable handling of nullable values","archived":false,"fork":false,"pushed_at":"2024-08-19T13:59:32.000Z","size":169,"stargazers_count":1855,"open_issues_count":8,"forks_count":238,"subscribers_count":23,"default_branch":"main","last_synced_at":"2024-10-29T14:38:20.131Z","etag":null,"topics":["go","json","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/guregu.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},"funding":{"github":"guregu"}},"created_at":"2014-08-28T15:10:56.000Z","updated_at":"2024-10-28T17:11:40.000Z","dependencies_parsed_at":"2024-02-11T20:15:53.937Z","dependency_job_id":"fb28a86e-c5da-4396-99d1-05a8431621df","html_url":"https://github.com/guregu/null","commit_stats":{"total_commits":79,"total_committers":10,"mean_commits":7.9,"dds":0.2784810126582279,"last_synced_commit":"25a9e713755c3bf59e5d354c869eaab26cfacf02"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guregu%2Fnull","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guregu%2Fnull/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guregu%2Fnull/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guregu%2Fnull/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guregu","download_url":"https://codeload.github.com/guregu/null/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243806070,"owners_count":20350775,"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":["go","json","sql"],"created_at":"2024-07-31T16:00:45.424Z","updated_at":"2025-03-18T18:18:50.676Z","avatar_url":"https://github.com/guregu.png","language":"Go","readme":"## null [![GoDoc](https://godoc.org/github.com/guregu/null/v6?status.svg)](https://godoc.org/github.com/guregu/null/v6)\n`import \"github.com/guregu/null/v6\"`\n\nnull is a library with reasonable options for dealing with nullable SQL and JSON values\n\nThere are two packages: `null` and its subpackage `zero`.\n\nTypes in `null` will only be considered null on null input, and will JSON encode to `null`. If you need zero and null be considered separate values, use these.\n\nTypes in `zero` are treated like zero values in Go: blank string input will produce a null `zero.String`, and null Strings will JSON encode to `\"\"`. Zero values of these types will be considered null to SQL. If you need zero and null treated the same, use these.\n\n#### Interfaces\n\n- All types implement `sql.Scanner` and `driver.Valuer`, so you can use this library in place of `sql.NullXXX`.\n- All types also implement `json.Marshaler` and `json.Unmarshaler`, so you can marshal them to their native JSON representation.\n- All non-generic types implement `encoding.TextMarshaler`, `encoding.TextUnmarshaler`. A null object's `MarshalText` will return a blank string.\n- All types implement `interface { IsZero() bool }`. Combined with Go 1.24's `,omitzero`, this lets you omit null types from JSON.\n\n## null package\n\n`import \"github.com/guregu/null/v6\"`\n\n#### null.String\nNullable string.\n\nMarshals to JSON null if SQL source data is null. Zero (blank) input will not produce a null String.\n\n#### null.Int, null.Int32, null.Int16, null.Byte\nNullable int64/int32/int16/byte.\n\nMarshals to JSON null if SQL source data is null. Zero input will not produce a null Int.\n\n#### null.Float\nNullable float64.\n\nMarshals to JSON null if SQL source data is null. Zero input will not produce a null Float.\n\n#### null.Bool\nNullable bool.\n\nMarshals to JSON null if SQL source data is null. False input will not produce a null Bool.\n\n#### null.Time\n\nMarshals to JSON null if SQL source data is null. Zero input will not produce a null Time.\n\n#### null.Value\nGeneric nullable value.\n\nWill marshal to JSON null if SQL source data is null. Does not implement `encoding.TextMarshaler`.\n\n## zero package\n\n`import \"github.com/guregu/null/v6/zero\"`\n\n#### zero.String\nNullable string.\n\nWill marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent.\n\n#### zero.Int, zero.Int32, zero.Int16, zero.Byte\nNullable int64/int32/int16/byte.\n\nWill marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent.\n\n#### zero.Float\nNullable float64.\n\nWill marshal to 0.0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent.\n\n#### zero.Bool\nNullable bool.\n\nWill marshal to false if null. `false` produces a null Float. Null values and zero values are considered equivalent.\n\n#### zero.Time\nNullable time.\n\nWill marshal to the zero time if null. Uses `time.Time`'s marshaler.\n\n#### zero.Value[`T`]\nGeneric nullable value.\n\nWill marshal to zero value if null. `T` is required to be a comparable type. Does not implement `encoding.TextMarshaler`.\n\n## About\n\n### Q\u0026A\n\n#### Can you add support for other types?\nThis package is intentionally limited in scope. It will only support the types that [`driver.Value`](https://godoc.org/database/sql/driver#Value) supports. Feel free to fork this and add more types if you want.\n\n#### Can you add a feature that ____?\nThis package isn't intended to be a catch-all data-wrangling package. It is essentially finished. If you have an idea for a new feature, feel free to open an issue to talk about it or fork this package, but don't expect this to do everything.\n\n### Package history\n\n#### v6\n- Added `ValueOr` method to each type\n\n#### v5\n- Now a Go module under the path `github.com/guregu/null/v6`\n- Added missing types from `database/sql`: `Int32, Int16, Byte`\n- Added generic `Value[T]` embedding `sql.Null[T]`\n\n#### v4\n- Available at `gopkg.in/guregu/null.v4`\n- Unmarshaling from JSON `sql.NullXXX` JSON objects (e.g. `{\"Int64\": 123, \"Valid\": true}`) is no longer supported. It's unlikely many people used this, but if you need it, use v3.\n\n### Bugs\n- ~~`json`'s `\",omitempty\"` struct tag does not work correctly right now. It will never omit a null or empty String. This might be [fixed eventually](https://github.com/golang/go/issues/11939).~~\n  - As of Go 1.24, you can use `,omitzero` to omit this package's types. Only took 10 years!\n\n### License\nBSD\n","funding_links":["https://github.com/sponsors/guregu"],"categories":["开源类库","Go","Open source library","Repositories"],"sub_categories":["开发辅助包","Development Aid Package"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguregu%2Fnull","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguregu%2Fnull","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguregu%2Fnull/lists"}