push all exercices

This commit is contained in:
lbenedar
2026-02-21 14:09:13 +03:00
commit a94d994b8c
72 changed files with 787 additions and 0 deletions

BIN
ch9/1/ch9_1.exe Normal file

Binary file not shown.

33
ch9/1/ch9_1.go Normal file
View File

@@ -0,0 +1,33 @@
package main
import (
"errors"
"fmt"
)
type SentinelError string
func (s SentinelError) Error() string {
return string(s)
}
func (s SentinelError) Is(target error) bool {
return s.Error() == target.Error()
}
const (
ErrId = SentinelError("Wrong ID")
ErrTest = SentinelError("Wrong Test")
)
func main() {
errId := ErrId
errTest := ErrTest
fmt.Printf("Does ErrId found: %b\n", errors.Is(errId, ErrId))
fmt.Printf("Does ErrTest found: %b\n", errors.Is(errId, ErrTest))
fmt.Printf("Does ErrId found: %b\n", errors.Is(errTest, ErrId))
fmt.Printf("Does ErrTest found: %b\n", errors.Is(errTest, ErrTest))
}

3
ch9/1/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ch9_1
go 1.25.0

BIN
ch9/2/ch9_2.exe Normal file

Binary file not shown.

65
ch9/2/ch9_2.go Normal file
View 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
View File

@@ -0,0 +1,3 @@
module ch9_2
go 1.25.0