Skip to content

Commit

Permalink
feat: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
WaterLemons2k committed Jun 28, 2024
1 parent 7cf6157 commit c4dded4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
pull_request:

jobs:
build:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# must

Wraps a call to a function returning (T, error)
and panics if the error is non-nil.
Provides a wrapper for calls to a function returning (T, error)

Similar to [`template.Must`](https://golang.org/pkg/text/template/#Must).

## Installation

Expand All @@ -11,16 +12,4 @@ go get github.com/g-must/must

## Usage

```go
package main

import (
"net/http"

. "github.com/g-must/must"
)

func main() {
_ = Must(http.Get(""))
}
```
See the [examples](example_test.go).
26 changes: 26 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package must_test

import (
"fmt"
"os"

. "github.com/g-must/must"
)

func ExampleMust() {
f := Must(os.Stat("."))

fmt.Println(f.Name())
// Output: .
}

func ExampleMust_error() {
defer func() {
if r := recover(); r != nil {
fmt.Println("error")
}
}()

Must(os.Stat(""))
// Output: error
}
1 change: 1 addition & 0 deletions must.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package must provides a wrapper for calls to a function returning (T, error)
package must

// Must wraps a call to a function returning (T, error)
Expand Down
8 changes: 3 additions & 5 deletions must_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"testing"
)

type data struct {
var testData = struct {
v any
err error
}

var testData = data{
}{
v: 1,
err: errors.New(""),
}
Expand All @@ -29,7 +27,7 @@ func TestMust(t *testing.T) {
panic: false,
},
{
name: "panic",
name: "error",
f: func() (any, error) {
return nil, testData.err
},
Expand Down

0 comments on commit c4dded4

Please # to comment.