{"id":17118935,"url":"https://github.com/chyroc/anyhow","last_synced_at":"2025-03-24T02:18:51.357Z","repository":{"id":94090961,"uuid":"471892772","full_name":"chyroc/anyhow","owner":"chyroc","description":"error handle","archived":false,"fork":false,"pushed_at":"2024-04-19T04:21:16.000Z","size":49,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-29T08:22:54.193Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chyroc.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}},"created_at":"2022-03-20T05:58:37.000Z","updated_at":"2023-12-03T13:22:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"56a3a5f7-6868-45b7-bba9-feed61aeb10c","html_url":"https://github.com/chyroc/anyhow","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chyroc%2Fanyhow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chyroc%2Fanyhow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chyroc%2Fanyhow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chyroc%2Fanyhow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chyroc","download_url":"https://codeload.github.com/chyroc/anyhow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245195962,"owners_count":20575938,"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":[],"created_at":"2024-10-14T17:55:49.240Z","updated_at":"2025-03-24T02:18:51.335Z","avatar_url":"https://github.com/chyroc.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# anyhow\n\ngo impl for https://docs.rs/anyhow/latest/anyhow/\n\n[![codecov](https://codecov.io/gh/chyroc/anyhow/branch/master/graph/badge.svg)](https://codecov.io/gh/chyroc/anyhow)\n[![go report card](https://goreportcard.com/badge/github.com/chyroc/anyhow \"go report card\")](https://goreportcard.com/report/github.com/chyroc/anyhow)\n[![test status](https://github.com/chyroc/anyhow/actions/workflows/test.yml/badge.svg)](https://github.com/chyroc/anyhow/actions)\n[![Apache-2.0 license](https://img.shields.io/badge/License-Apache%202.0-brightgreen.svg)](https://opensource.org/licenses/Apache-2.0)\n[![Go.Dev reference](https://img.shields.io/badge/go.dev-reference-blue?logo=go\u0026logoColor=white)](https://pkg.go.dev/github.com/chyroc/anyhow)\n[![Go project version](https://badge.fury.io/go/github.com%2Fchyroc%2Fanyhow.svg)](https://badge.fury.io/go/github.com%2Fchyroc%2Fanyhow)\n\n## Installation\n\n```shell\ngo get github.com/chyroc/anyhow\n```\n\n## Features\n\n- result struct\n  - `Result1[T]`\n  - `Result2[T1, T2]`\n  - `Result3[T1, T2, T3]`\n  - `Result4[T1, T2, T3, T4]`\n  - `Result5[T1, T2, T3, T4, T5]`\n  - `Result6[T1, T2, T3, T4, T5, T6]`\n- functions:\n  - `And(self R[T], res R[U]) R[U]`: Returns res if the result is Ok, otherwise returns the Err value of self\n  - `AndThen(self R[T], op (T) -\u003e R[U]) R[U]`: Calls op if the result is Ok, otherwise returns the Err value of self\n  - `Map(self R[T], op (T) -\u003e U) R[U]`: Maps a Result\u003cT\u003e to Result\u003cU\u003e by applying a function to a contained Ok value, leaving an Err value untouched\n  - `MapOr(self R[T], default U, op func(T) U) U`: Returns the provided default (if Err), or applies a function to the contained value (if Ok)\n- methods:\n  - `(R[T]) IntoErr() error`: Returns the contained Err value, but never panics\n  - `(R[T]) IntoOk() T1`: Returns the contained Ok value, but never panics\n  - `(R[T]) Expect(string) T`: Returns the contained Ok value, otherwise panics with a panic message including the passed message, and the content of the Err\n  - `(R[T]) ExpectErr(string) error`: Returns the contained Err value, otherwise panics with a panic message including the passed message, and the content of the Ok\n  - `(R[T]) Inspect(f (T) -\u003e void) R[T]`: Calls a function with a reference to the contained value if Ok\n  - `(R[T]) InspectErr(f (error) -\u003e void) R[T]`: Calls a function with a reference to the contained value if Err\n  - `(R[T]) MapErr(op func(error) error) R[T]`: Maps a R[T] to R[T] by applying a function to a contained Err value, leaving an Ok value untouched\n  - `(R[T]) IsErr() bool`: Returns true if the result is Err\n  - `(R[T]) IsOk() bool`: Returns true if the result is Ok\n  - `(R[T]) Or(res R[T]) R[T]`: Returns res if the result is Err, otherwise returns the Ok value of self\n  - `(R[T]) OrElse(op (error) -\u003e R[T]) R[T]`: Calls op if the result is Err, otherwise returns the Ok value of self\n  - `(R[T]) Unwrap() T`: Returns the contained Ok value, otherwise panics with Err\n  - `(R[T]) UnwrapOr(default T) T`: Returns the contained Ok value or a provided default\n  - `(R[T]) UnwrapOrDefault() T`: Returns the contained Ok value or a default value\n  - `(R[T]) UnwrapOrElse(op (error) -\u003e T) T`: Returns the contained Ok value or computes it from a closure\n\n## Usage\n\n```go\npackage main\n\nimport (\n  \"log\"\n  \"os/user\"\n\n  . \"github.com/chyroc/anyhow\"\n)\n\nfunc getHomePath() Result1[string] {\n  user, err := user.Current()\n  if err != nil {\n    return Err1[string](err)\n  }\n  return Ok1(user.HomeDir)\n}\n\nfunc Example_AndThen_UnwrapOr() {\n  // get home path, and then get log path, or use /tmp/log\n  _ = AndThen11(getHomePath(), func(t1 string) Result1[string] {\n    return Ok1(t1 + \"/.log\")\n  }).UnwrapOr(\"/tmp/log\")\n}\n\nfunc Example_Inspect_InspectErr() {\n  // get home path, log it, or log error\n  _ = getHomePath().Inspect(func(t1 string) {\n    log.Println(\"home:\", t1)\n  }).InspectErr(func(err error) {\n    log.Println(\"error:\", err)\n  }).UnwrapOr(\"/tmp/log\")\n}\n\nfunc Example_Expect_ExpectErr() {\n  // get home path, or panic with `must get home` msg\n  _ = getHomePath().Expect(\"must get home\")\n\n  // get home path with err, or panic with `must get err` msg\n  _ = getHomePath().ExpectErr(\"must get err\")\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchyroc%2Fanyhow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchyroc%2Fanyhow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchyroc%2Fanyhow/lists"}