Debian Codegraph @

errors/example_test.go

raw symbols SCIP
1 // Copyright 2012 The Go Authors. 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 "fmt"
9 "os"
10 "time"
11
12 "golang.org/x/exp/errors"
13 )
14
15 // MyError is an error implementation that includes a time and message.
16 type MyError struct {
17 When time.Time
18 What string
19 }
20
21 func (e MyError) Error() string {
22 return fmt.Sprintf("%v: %v", e.When, e.What)
23 }
24
25 func oops() error {
26 return MyError{
27 time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
28 "the file system has gone away",
29 }
30 }
31
32 func Example() {
33 if err := oops(); err != nil {
34 fmt.Println(err)
35 }
36 // Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away
37 }
38
39 func ExampleAs() {
40 _, err := os.Open("non-existing")
41 if err != nil {
42 var pathError *os.PathError
43 if errors.As(err, &pathError) {
44 fmt.Println("Failed at path:", pathError.Path)
45 }
46 }
47
48 // Output:
49 // Failed at path: non-existing
50 }