push all exercices
This commit is contained in:
BIN
ch9/2/ch9_2.exe
Normal file
BIN
ch9/2/ch9_2.exe
Normal file
Binary file not shown.
65
ch9/2/ch9_2.go
Normal file
65
ch9/2/ch9_2.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type CustomError struct {
|
||||
fieldName string
|
||||
}
|
||||
|
||||
type CustomError2 struct {
|
||||
fieldName string
|
||||
}
|
||||
|
||||
func (c CustomError) Error() string {
|
||||
return fmt.Sprintf("missing field: %s", c.fieldName)
|
||||
}
|
||||
|
||||
func (c CustomError) Is(target error) bool {
|
||||
return c.Error() == target.Error()
|
||||
}
|
||||
|
||||
func (c CustomError2) Error() string {
|
||||
return fmt.Sprintf("missing field: %s", c.fieldName)
|
||||
}
|
||||
|
||||
func (c CustomError2) Is(target error) bool {
|
||||
return c.Error() == target.Error()
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
test1 := CustomError{"Test1"}
|
||||
test2 := CustomError2{"Test2"}
|
||||
test3 := fmt.Errorf("Test3 %w", test1)
|
||||
test4 := fmt.Errorf("Test4: %w", test3)
|
||||
|
||||
if errors.As(test3, &test1) {
|
||||
fmt.Println("Test3 contains Test1")
|
||||
fmt.Printf("Test3 contains Test1 field: %s", test3.Error())
|
||||
} else {
|
||||
fmt.Println("Test3 doesn't contain Test1")
|
||||
}
|
||||
if errors.As(test3, &test2) {
|
||||
fmt.Println("Test3 contains Test2")
|
||||
} else {
|
||||
fmt.Println("Test3 doesn't contain Test1")
|
||||
}
|
||||
if errors.As(test4, &test1) {
|
||||
fmt.Println("Test4 contains Test1")
|
||||
} else {
|
||||
fmt.Println("Test3 doesn't contain Test1")
|
||||
}
|
||||
if errors.As(test4, &test2) {
|
||||
fmt.Println("Test4 contains Test2")
|
||||
} else {
|
||||
fmt.Println("Test3 doesn't contain Test1")
|
||||
}
|
||||
if errors.As(test4, &test3) {
|
||||
fmt.Println("Test4 contains Test3")
|
||||
} else {
|
||||
fmt.Println("Test3 doesn't contain Test1")
|
||||
}
|
||||
}
|
||||
3
ch9/2/go.mod
Normal file
3
ch9/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ch9_2
|
||||
|
||||
go 1.25.0
|
||||
Reference in New Issue
Block a user