github.com/searKing/golang/go@v1.2.117/errors/example_test.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package errors_test
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  
    11  	errors_ "github.com/searKing/golang/go/errors"
    12  )
    13  
    14  func ExampleMulti() {
    15  	err1 := errors.New("err1")
    16  	err2 := errors.New("err2")
    17  	err3 := errors.New("err3")
    18  	err := errors.Join(err1, err2)
    19  	fmt.Println(err)
    20  	if errors.Is(err, err1) {
    21  		fmt.Println("err is err1")
    22  	}
    23  	if errors.Is(err, err2) {
    24  		fmt.Println("err is err2")
    25  	}
    26  	if errors.Is(err, nil) {
    27  		fmt.Println("err is nil")
    28  	}
    29  	if errors.Is(err, err3) {
    30  		fmt.Println("err is err3")
    31  	}
    32  	// Output:
    33  	// err1
    34  	// err2
    35  	// err is err1
    36  	// err is err2
    37  }
    38  
    39  func ExampleMark() {
    40  	err := errors.New("err")
    41  	mark1 := errors.New("mark1")
    42  	mark2 := errors.New("mark2")
    43  	mark3 := errors.New("mark3")
    44  	me := errors_.Mark(err, mark1, mark2)
    45  	fmt.Println(me)
    46  	if errors.Is(me, err) {
    47  		fmt.Println("err is err")
    48  	}
    49  	if errors.Is(me, mark1) {
    50  		fmt.Println("err is mark1")
    51  	}
    52  	if errors.Is(me, mark2) {
    53  		fmt.Println("err is mark2")
    54  	}
    55  	if errors.Is(me, mark3) {
    56  		fmt.Println("err is mark3")
    57  	}
    58  	if errors.Is(me, nil) {
    59  		fmt.Println("err is nil")
    60  	}
    61  	// Output:
    62  	// err
    63  	// err is err
    64  	// err is mark1
    65  	// err is mark2
    66  }