{"id":17321822,"url":"https://github.com/earthboundkid/truthy","last_synced_at":"2025-09-03T04:40:45.953Z","repository":{"id":40987624,"uuid":"405280155","full_name":"earthboundkid/truthy","owner":"earthboundkid","description":"Package truthy provides truthy condition testing with Go generics","archived":false,"fork":false,"pushed_at":"2024-01-09T14:27:07.000Z","size":31,"stargazers_count":33,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-04T09:05:13.703Z","etag":null,"topics":["generics","go","golang","truthy","truthy-checks"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"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/earthboundkid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"carlmjohnson"}},"created_at":"2021-09-11T04:11:40.000Z","updated_at":"2024-02-15T15:16:16.000Z","dependencies_parsed_at":"2024-01-25T05:27:59.151Z","dependency_job_id":"680e58d3-deeb-49e0-a950-6c3e1b9da9ec","html_url":"https://github.com/earthboundkid/truthy","commit_stats":null,"previous_names":["earthboundkid/truthy","carlmjohnson/truthy"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/earthboundkid/truthy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Ftruthy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Ftruthy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Ftruthy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Ftruthy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/earthboundkid","download_url":"https://codeload.github.com/earthboundkid/truthy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/earthboundkid%2Ftruthy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273392282,"owners_count":25097257,"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","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"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":["generics","go","golang","truthy","truthy-checks"],"created_at":"2024-10-15T13:39:50.684Z","updated_at":"2025-09-03T04:40:45.908Z","avatar_url":"https://github.com/earthboundkid.png","language":"Go","funding_links":["https://github.com/sponsors/carlmjohnson"],"categories":[],"sub_categories":[],"readme":"# Truthy [![Go Reference](https://pkg.go.dev/badge/github.com/carlmjohnson/truthy.svg)](https://pkg.go.dev/github.com/carlmjohnson/truthy)\n\n![Truthiness](https://user-images.githubusercontent.com/222245/136619462-f2bc5858-067f-4277-a813-b95c64b3cdac.png)\n\nTruthy is a package which uses generics to create useful boolean tests of truthiness and helper functions.\n\n## Examples\n\n```\n// truthy.Value returns whether a comparable value\n// is equal to the zero value for its type.\n//\n// truthy.ValueAny returns the truthiness of any argument.\n// If the value's type has a Bool() bool method, the method is called and returned.\n// If the type has an IsZero() bool method, the opposite value is returned.\n// Slices and maps are truthy if they have a length greater than zero.\n// All other types are truthy if they are not their zero value.\n\ntruthy.Value(0) // false\ntruthy.Value(1) // true\n\ntruthy.Value(\"\") // false\ntruthy.Value(\" \") // true\n\ntruthy.ValueSlice([]byte(``)) // false\ntruthy.ValueSlice([]byte(` `)) // true\n\ntruthy.ValueSlice([]int{}) // false\ntruthy.ValueSlice([]int{1, 2, 3}) // true\n\nvar err error\ntruthy.Value(err) // false\ntruthy.Value(errors.New(\"hi\")) // true\nif truthy.Value(err) {\n\tpanic(err)\n}\n\n\nvar p *int\ntruthy.Value(p) // false\n\np = new(int)\n// truthy does not check value underlying pointer!\ntruthy.Value(p) // true\n\n// Ever wish Go had ? : ternary operators?\n// Now it has a ternary function.\nx := truthy.Cond(truthy.Value(\"\"), 1, 10) // x == 10\n\n// truthy.Cond cannot lazily evaluate its arguments,\n// but you can use a closure to fake it.\ns := truthy.Cond(truthy.ValueSlice([]string{\"\"}),\n\tfunc() string {\n\t\t// do some calculation\n\t\treturn \"foo\"\n\t},\n\tfunc() string {\n\t\t// do some calculation\n\t\treturn \"bar\"\n\t})()\n// s == \"foo\"\n\n\n// How about an equivalent of the nullish coalescing operator ??\n// as seen in C#, JavaScript, PHP, etc.:\nvar s string\ntruthy.First(s, \"default\") // \"default\"\ns = \"something\"\ntruthy.First(s, \"default\") // \"something\"\ntruthy.First(0, 0*1, 1-1, 0x10-10) // 6\n\n// Easily set defaults\nn := getUserInput()\ntruthy.SetDefault(\u0026n, 42)\n```\n\n## FAQs\n\n### Oh god\n\nThis is the correct reaction.\n\n### Isn't this just using reflection? Does it even really require generics?\n\nI tried to write a non-generic version of this package first, but you can’t reflect on an interface type. When you do `reflect.Value(x)`, you lose the fact that x was, e.g. an error, because `reflect.Value()` only takes `interface{}` and the conversion loses the interface type. You’d end up saying whether the underlying concrete type was empty or not, which is typically not what you want. To work around that, you could require that everything is passed as a pointer, e.g. `reflect.Value(\u0026err)`, but `truthy.Value(\u0026err)` sucks as an API. If you look at how `truthy.Value()` works, it accepts a value of type `T`, and then passes `*T` to `reflect.Value()` and calls `value.Elem()` to finally get the correct reflection type. So, on a technical level, you couldn’t quite make this API work without generics, although it could be close. However, `truthy.Filter()`, `truthy.SetDefault()`, `truthy.Any()`, and `truthy.All()` could be implemented with pure reflection, although the implementation would be a lot uglier.\n\nThen there’s `truthy.First()`. To be honest, `truthy.First()` is the only part of the package that I consider actually useful, and even that, I mostly expect it to be used for picking a string or default. Anyhow, it requires generics to avoid the cast back from interface type to the concrete type.\n\n### Should I use this package?\nProbably not. It's a little bit of a joke package, but the `truthy.First()` and `truthy.SetDefault()` functionality seem useful, especially for strings. Time will tell what best practices around the use of generics in Go turn out to be.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fearthboundkid%2Ftruthy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fearthboundkid%2Ftruthy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fearthboundkid%2Ftruthy/lists"}