From 03b82d961b27214c3ba1af37ebe682ad355f7e6d Mon Sep 17 00:00:00 2001 From: yangyile Date: Fri, 22 Nov 2024 02:29:29 +0700 Subject: [PATCH] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E4=BF=AE=E6=94=B9=E6=96=87?= =?UTF-8?q?=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 49 +++++++++++++++++++++++++++++-------------------- nc_test.go | 6 +++++- np_test.go | 6 +++++- nv_test.go | 6 +++++- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 68a086d..9673bb2 100644 --- a/README.md +++ b/README.md @@ -140,33 +140,34 @@ See [demo2](internal/demos/demo2/main.go) This way, you can check if the error exists at each step and continue with the result without writing long error-handling logic. -### Example Usage +### Example NPC + +This section demonstrates how you can simplify error handling and result validation using the `done` package. -This section demonstrates how you can simplify error handling and result validation using the `done` package. By using `done.P1`, `done.P2`, and `done.P3`, you can reduce the complexity of your error checks and nil checks, making your code more readable. +By using `done.V1`, `done.P2`, and `done.C3`, you can reduce the complexity of your error checks and nil checks, making your code more readable. -#### Example 1: Checking Results from `run1()` +#### Example NPC 1 (nv): Checking Results from `run1()` -Before using `done.P1`, we would have to manually check the error and handle nil values for the result: +Before using `done.V1`, we would have to manually check the error and handle nil values for the result: ```go a, err := run1() if err != nil { panic(err) // If an error occurs, trigger a panic } -if a == nil { - panic("a is nil") // If 'a' is nil, trigger panic -} fmt.Println(a) // Output the result ``` -With `done.P1`, you can perform the same checks in a cleaner, more concise way: +With `done.V1`, you can perform the same checks in a cleaner, more concise way: ```go -a := done.P1(run1()) // done.P1 handles error checking and nil validation +a := done.V1(run1()) // done.V1 handles error checking fmt.Println(a) // Output the result ``` -#### Example 2: Handling Multiple Results from `run2()` +see [code](nv.go) see [case](nv_test.go) + +#### Example NPC 2 (np): Handling Multiple Results from `run2()` For functions that return multiple values, such as `run2()`, you can use a similar approach: @@ -187,11 +188,13 @@ fmt.Println(a, b) // Output both 'a' and 'b' With `done.P2`, the error handling and nil checks are simplified: ```go -a, b := done.P2(run2()) // done.P2 simplifies error checking and validation +a, b := done.P2(run2()) // done.P2 simplifies error checking and nil validation fmt.Println(a, b) // Output both 'a' and 'b' ``` -#### Example 3: Handling Multiple Results from `run3()` +see [code](np.go) see [case](np_test.go) + +#### Example NPC 3 (nc): Handling Multiple Results from `run3()` For functions that return even more values, like `run3()`, you can extend this pattern: @@ -200,33 +203,39 @@ a, b, c, err := run3() if err != nil { panic(err) // If an error occurs, trigger a panic } -if a == nil { - panic("a is nil") // If 'a' is nil, trigger panic +if a == 0 { + panic("a is zero") // If 'a' is zero, trigger panic } -if b == nil { - panic("b is nil") // If 'b' is nil, trigger panic +if b == "" { + panic("b is zero") // If 'b' is zero, trigger panic } -if c == nil { - panic("c is nil") // If 'c' is nil, trigger panic +if c == zero { + panic("c is zero") // If 'c' is zero, trigger panic } fmt.Println(a, b, c) // Output 'a', 'b', and 'c' ``` -Using `done.P3` simplifies error handling and validation for multiple results: +Using `done.C3` simplifies error handling and validation for multiple results: ```go -a, b, c := done.P3(run3()) // done.P3 simplifies error checking and validation +a, b, c := done.C3(run3()) // done.C3 simplifies error checking and no-zero validation fmt.Println(a, b, c) // Output 'a', 'b', and 'c' ``` +see [code](nc.go) see [case](nc_test.go) + See [demo3](internal/demos/demo3/main.go) This way you can avoid checking whether the return value has errors or null values, which is very convenient. There are also these methods that can provide additional convenience. + `done.P1() - done.P9()` + `done.C1() - done.C9()` + `done.V1() - done.V9()` + This avoids always checking if there are errors in the return values. ## Usage Scenarios diff --git a/nc_test.go b/nc_test.go index 1a0c7c9..b2e056a 100644 --- a/nc_test.go +++ b/nc_test.go @@ -8,7 +8,11 @@ import ( ) func TestC0(t *testing.T) { - done.C0(error(nil)) + run := func() error { + return nil + } + + done.C0(run()) } func TestC1(t *testing.T) { diff --git a/np_test.go b/np_test.go index ec30765..ed1cc2a 100644 --- a/np_test.go +++ b/np_test.go @@ -8,7 +8,11 @@ import ( ) func TestP0(t *testing.T) { - done.P0(error(nil)) + run := func() error { + return nil + } + + done.P0(run()) } func TestP1(t *testing.T) { diff --git a/nv_test.go b/nv_test.go index 2114623..d5d1f02 100644 --- a/nv_test.go +++ b/nv_test.go @@ -8,7 +8,11 @@ import ( ) func TestV0(t *testing.T) { - done.V0(error(nil)) + run := func() error { + return nil + } + + done.V0(run()) } func TestV1(t *testing.T) {