{"id":47116961,"url":"https://github.com/xiegeo/must","last_synced_at":"2026-03-12T19:03:58.581Z","repository":{"id":64497589,"uuid":"575917177","full_name":"xiegeo/must","owner":"xiegeo","description":"When you don't need error handling","archived":false,"fork":false,"pushed_at":"2023-01-11T12:49:01.000Z","size":20,"stargazers_count":5,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-06-21T14:21:51.021Z","etag":null,"topics":["assertions","contract","generic","golang","method-chaining","must","panic","testing-tools"],"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/xiegeo.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}},"created_at":"2022-12-08T15:36:06.000Z","updated_at":"2024-01-12T18:52:31.000Z","dependencies_parsed_at":"2023-02-09T03:00:43.940Z","dependency_job_id":null,"html_url":"https://github.com/xiegeo/must","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/xiegeo/must","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiegeo%2Fmust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiegeo%2Fmust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiegeo%2Fmust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiegeo%2Fmust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xiegeo","download_url":"https://codeload.github.com/xiegeo/must/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiegeo%2Fmust/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30439358,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T14:34:45.044Z","status":"ssl_error","status_checked_at":"2026-03-12T14:09:33.793Z","response_time":114,"last_error":"SSL_read: 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":["assertions","contract","generic","golang","method-chaining","must","panic","testing-tools"],"created_at":"2026-03-12T19:03:58.395Z","updated_at":"2026-03-12T19:03:58.573Z","avatar_url":"https://github.com/xiegeo.png","language":"Go","readme":"# must [![Go Reference](https://pkg.go.dev/badge/.svg)](https://pkg.go.dev/github.com/xiegeo/must)\n\nWhen you don't need error handling\n\nI found myself using this snipped in a few places.\n\n``` go\nfunc must[T any](out T, err error) T {\n    if err != nil {\n        panic(err)\n    }\n    return out\n}\n```\n\nOccasionally a little more, so made a home for them, DRY.\n\n## Scope\n\n- Error handling is not always necessary. Panic on error should be\n  easier than ignoring them.\n- Lightweight assertions. No deps, 3 digit loc count.\n- `must` is designed to stand out like `unsafe`.\n  So normal error handling and none panicky helper functions should live else where.\n  \n### Turn errors into panics\n\n``` go\n// NoError panics on error.\n//\n// You can use this instead of ignoring errors that never happen by contract.\nfunc NoError(err error) {\n    if err != nil {\n        panic(err)\n    }\n}\n\n// Value panics on error, otherwise returns the first value.\n//\n// Use this instead of writing a Must version of your function.\nfunc Value[T any](out T, err error) T {\n    if err != nil {\n        panic(err)\n    }\n    return out\n}\n```\n\nAlso `Value2` is available for longer function signatures.\n`V` and `V2` are their short aliases since the `must.` prefix already stands out enough.\n\n### Turn assertions into panics\n\nFunctions like `True(bool)` and friends. To assert a condition or panic.\n\n`B1`, `B2`... are generic assertions that pass their first inputs unmodified with compile time type info preserved.\nThey return a function to specify what assertions are made on each field.\n\n``` go\nfunc ExampleB3() {\n    // lets say we have an io.RuneReader\n    readRune := func() (r rune, size int, err error) {\n        return 'a', 1, nil\n    }\n\n    // that must return 1 byte runes and never err out\n    char, _, _ := must.B3(readRune())(must.Any, 1, nil)\n    fmt.Println(string(char))\n\n    // output:\n    // a\n}               \n```\n\nAssertions have optional debug arguments, to provide additional information when\nviolated. Usually, just pass in the line comment as string.\n\n### Usage in unit tests\n\n`must.*T#` will cause the function from using panic to using t.Fatal on the provided unit test interface.\nAs an alternative to [testify/require](https://pkg.go.dev/github.com/stretchr/testify/require) that allows value chaining.\n\nFor the functions that accept `debug ...any`, if the first debug value supports t.Fatal and t.Helper,\nthen panic will be changed to calling t.Fatal instead. (This is experimental)\n\nUse of testify or other alterative targeting unit testing should be preferred over must, expectably outside of test setup.\n\n### Panic test helpers\n\n`must.Panic` and `must.Recover`. Useful for writing tests for panicky cases.\n\n## Compatibility\n\nSince this library is heavily dependent on generics. **1.18** is the minimum Go version supported until a new go version has a feature too good to pass.\n\nTagged versions follow **sematic versioning**. Untagged master/main branch is for development.\n\nValues used in panics are only for debugging. No guarantees are provided on how panics are constructed. This means the recovered value and stack trace could change even in bugfix versions.\n\n## Version 1.0 milestone\n\n- Gain enough downstream usage cases to prove API fitness and stability.\n\n## Alternatives \n\nSee [wiki](https://github.com/xiegeo/must/wiki/Alternatives)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiegeo%2Fmust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiegeo%2Fmust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiegeo%2Fmust/lists"}