commit a94d994b8c0fa6265c7153e6e8b95db5d1f015e8 Author: lbenedar Date: Sat Feb 21 14:09:13 2026 +0300 push all exercices diff --git a/ch1/Makefile b/ch1/Makefile new file mode 100644 index 0000000..d283a18 --- /dev/null +++ b/ch1/Makefile @@ -0,0 +1,15 @@ +.DEFAULT_GOAL := build + +fmt: + go fmt ./... + +vet: fmt + go vet ./... + +build: vet + go build + +clean: + go clean + +.PHONY:fmt vet build \ No newline at end of file diff --git a/ch1/go.mod b/ch1/go.mod new file mode 100644 index 0000000..41f64d0 --- /dev/null +++ b/ch1/go.mod @@ -0,0 +1,3 @@ +module hello_world + +go 1.25.0 diff --git a/ch1/hello.go b/ch1/hello.go new file mode 100644 index 0000000..a8b251c --- /dev/null +++ b/ch1/hello.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main() { + + fmt.Printf("Hello ttt sad , %s!\n", "world") + +} diff --git a/ch1/hello_world.exe b/ch1/hello_world.exe new file mode 100644 index 0000000..0af6c7d Binary files /dev/null and b/ch1/hello_world.exe differ diff --git a/ch2/1/ch2.exe b/ch2/1/ch2.exe new file mode 100644 index 0000000..5136dff Binary files /dev/null and b/ch2/1/ch2.exe differ diff --git a/ch2/1/const_declarations.go b/ch2/1/const_declarations.go new file mode 100644 index 0000000..c0b9eec --- /dev/null +++ b/ch2/1/const_declarations.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +func main() { + var i int64 = 20 + var f float64 = float64(i) + fmt.Println(i) + fmt.Println(f) +} diff --git a/ch2/1/go.mod b/ch2/1/go.mod new file mode 100644 index 0000000..41c1baf --- /dev/null +++ b/ch2/1/go.mod @@ -0,0 +1,3 @@ +module ch2 + +go 1.25.0 diff --git a/ch2/2/ch2_2.exe b/ch2/2/ch2_2.exe new file mode 100644 index 0000000..7cf663a Binary files /dev/null and b/ch2/2/ch2_2.exe differ diff --git a/ch2/2/ch2_2.go b/ch2/2/ch2_2.go new file mode 100644 index 0000000..f841cad --- /dev/null +++ b/ch2/2/ch2_2.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + const value = 15 + var i int = value + var f float64 = value + fmt.Println(i) + fmt.Println(f) +} diff --git a/ch2/2/go.mod b/ch2/2/go.mod new file mode 100644 index 0000000..bbc9be6 --- /dev/null +++ b/ch2/2/go.mod @@ -0,0 +1,3 @@ +module ch2_2 + +go 1.25.0 diff --git a/ch2/3/ch2_3.exe b/ch2/3/ch2_3.exe new file mode 100644 index 0000000..745bf94 Binary files /dev/null and b/ch2/3/ch2_3.exe differ diff --git a/ch2/3/ch3_2.go b/ch2/3/ch3_2.go new file mode 100644 index 0000000..96bf79f --- /dev/null +++ b/ch2/3/ch3_2.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +func main() { + var b byte = 255 + var smallI int32 = 2147483647 + var bigI uint64 = 18446744073709551615 + b += 1 + smallI += 1 + bigI += 1 + fmt.Println(b) + fmt.Println(smallI) + fmt.Println(bigI) +} diff --git a/ch2/3/go.mod b/ch2/3/go.mod new file mode 100644 index 0000000..1b91717 --- /dev/null +++ b/ch2/3/go.mod @@ -0,0 +1,3 @@ +module ch2_3 + +go 1.25.0 diff --git a/ch3/1/ch3_1.exe b/ch3/1/ch3_1.exe new file mode 100644 index 0000000..dc2a825 Binary files /dev/null and b/ch3/1/ch3_1.exe differ diff --git a/ch3/1/ch3_1.go b/ch3/1/ch3_1.go new file mode 100644 index 0000000..ab050f4 --- /dev/null +++ b/ch3/1/ch3_1.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + var greetings = []string{"Hello", "Hola", "नमस्कार", "こんにちは", "Привіт"} + var a = greetings[:2] + var b = greetings[1:4] + var c = greetings[3:] + fmt.Println(a) + fmt.Println(b) + fmt.Println(c) +} diff --git a/ch3/1/go.mod b/ch3/1/go.mod new file mode 100644 index 0000000..7341203 --- /dev/null +++ b/ch3/1/go.mod @@ -0,0 +1,3 @@ +module ch3_1 + +go 1.25.0 diff --git a/ch3/2/ch3_2.exe b/ch3/2/ch3_2.exe new file mode 100644 index 0000000..d7c0d2c Binary files /dev/null and b/ch3/2/ch3_2.exe differ diff --git a/ch3/2/ch3_2.go b/ch3/2/ch3_2.go new file mode 100644 index 0000000..995ad9e --- /dev/null +++ b/ch3/2/ch3_2.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main() { + var message string = "Hi 👩 and 👨" + var msgRunes []rune = []rune(message) + fmt.Println(string(msgRunes[3])) +} diff --git a/ch3/2/go.mod b/ch3/2/go.mod new file mode 100644 index 0000000..09af834 --- /dev/null +++ b/ch3/2/go.mod @@ -0,0 +1,3 @@ +module ch3_2 + +go 1.25.0 diff --git a/ch3/3/ch3_3.exe b/ch3/3/ch3_3.exe new file mode 100644 index 0000000..9582926 Binary files /dev/null and b/ch3/3/ch3_3.exe differ diff --git a/ch3/3/ch3_3.go b/ch3/3/ch3_3.go new file mode 100644 index 0000000..542075a --- /dev/null +++ b/ch3/3/ch3_3.go @@ -0,0 +1,29 @@ +package main + +import "fmt" + +func main() { + type Employee struct { + firstName string + lastName string + id int + } + + firstVal := Employee{ + "Ivan", + "Ivanov", + 1, + } + secondVal := Employee{ + firstName: "Peter", + lastName: "Petrov", + id: 2, + } + var thirdVal Employee + thirdVal.firstName = "Sergei" + thirdVal.lastName = "Sidorov" + thirdVal.id = 3 + fmt.Println(firstVal) + fmt.Println(secondVal) + fmt.Println(thirdVal) +} diff --git a/ch3/3/go.mod b/ch3/3/go.mod new file mode 100644 index 0000000..056829e --- /dev/null +++ b/ch3/3/go.mod @@ -0,0 +1,3 @@ +module ch3_3 + +go 1.25.0 diff --git a/ch4/1/ch4_1.exe b/ch4/1/ch4_1.exe new file mode 100644 index 0000000..fce49c5 Binary files /dev/null and b/ch4/1/ch4_1.exe differ diff --git a/ch4/1/ch4_1.go b/ch4/1/ch4_1.go new file mode 100644 index 0000000..f2d685c --- /dev/null +++ b/ch4/1/ch4_1.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "math/rand" +) + +func main() { + randSlice := make([]int64, 100) + for i := 0; i < 100; i++ { + randSlice[i] = rand.Int63n(100) + } + fmt.Println(randSlice) +} diff --git a/ch4/1/go.mod b/ch4/1/go.mod new file mode 100644 index 0000000..314a1eb --- /dev/null +++ b/ch4/1/go.mod @@ -0,0 +1,3 @@ +module ch4_1 + +go 1.25.0 diff --git a/ch4/2/ch4_2.exe b/ch4/2/ch4_2.exe new file mode 100644 index 0000000..1f9ff20 Binary files /dev/null and b/ch4/2/ch4_2.exe differ diff --git a/ch4/2/ch4_2.go b/ch4/2/ch4_2.go new file mode 100644 index 0000000..9ab3fb6 --- /dev/null +++ b/ch4/2/ch4_2.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "math/rand" +) + +func main() { + const numElems = 10 + randSlice := make([]int64, numElems) + for i := 0; i < numElems; i++ { + randSlice[i] = rand.Int63n(100) + } + for _, v := range randSlice { + if v%6 == 0 { + fmt.Println("Six!") + } else if v%2 == 0 { + fmt.Println("Two!") + } else if v%3 == 0 { + fmt.Println("Three!") + } else { + fmt.Println("Never mind") + } + } + fmt.Println(randSlice) +} diff --git a/ch4/2/go.mod b/ch4/2/go.mod new file mode 100644 index 0000000..3d87261 --- /dev/null +++ b/ch4/2/go.mod @@ -0,0 +1,3 @@ +module ch4_2 + +go 1.25.0 diff --git a/ch4/3/ch4_3.exe b/ch4/3/ch4_3.exe new file mode 100644 index 0000000..a6b8330 Binary files /dev/null and b/ch4/3/ch4_3.exe differ diff --git a/ch4/3/ch4_3.go b/ch4/3/ch4_3.go new file mode 100644 index 0000000..eafa43c --- /dev/null +++ b/ch4/3/ch4_3.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + var total int + for i := 0; i < 10; i++ { + total = total + i + fmt.Println(total) + } + fmt.Println(total) +} diff --git a/ch4/3/go.mod b/ch4/3/go.mod new file mode 100644 index 0000000..79e4ec8 --- /dev/null +++ b/ch4/3/go.mod @@ -0,0 +1,3 @@ +module ch4_3 + +go 1.25.0 diff --git a/ch5/1/ch5_1.exe b/ch5/1/ch5_1.exe new file mode 100644 index 0000000..6dc0faa Binary files /dev/null and b/ch5/1/ch5_1.exe differ diff --git a/ch5/1/ch5_1.go b/ch5/1/ch5_1.go new file mode 100644 index 0000000..353f7f2 --- /dev/null +++ b/ch5/1/ch5_1.go @@ -0,0 +1,20 @@ +package main + +import ( + "errors" + "fmt" +) + +func simpleCalculator(dividend, divisor int) (int, error) { + if divisor == 0 { + err := errors.New("division by zero") + return 0, err + } + return dividend / divisor, nil +} + +func main() { + fmt.Println(simpleCalculator(5, 2)) + fmt.Println(simpleCalculator(5, 0)) + fmt.Println(simpleCalculator(17, 6)) +} diff --git a/ch5/1/go.mod b/ch5/1/go.mod new file mode 100644 index 0000000..3333979 --- /dev/null +++ b/ch5/1/go.mod @@ -0,0 +1,3 @@ +module ch5_1 + +go 1.25.0 diff --git a/ch5/2/ch5_2.exe b/ch5/2/ch5_2.exe new file mode 100644 index 0000000..79d178b Binary files /dev/null and b/ch5/2/ch5_2.exe differ diff --git a/ch5/2/ch5_2.go b/ch5/2/ch5_2.go new file mode 100644 index 0000000..d512f37 --- /dev/null +++ b/ch5/2/ch5_2.go @@ -0,0 +1,44 @@ +package main + +import ( + "errors" + "fmt" + "os" +) + +func fileLen(filename string) (int, error) { + const blockSize = 128 + file, err := os.Open(filename) + if err != nil { + fmt.Println("Could not open file!") + return 0, err + } + defer file.Close() + dataBlock := make([]byte, blockSize) + totalCount := 0 + for { + count, err := file.Read(dataBlock) + if err != nil { + return 0, err + } + totalCount += count + if count < blockSize { + break + } + } + return totalCount, nil +} + +func main() { + if len(os.Args) < 2 { + err := errors.New("Program doesn't have any arguments. Please pass argument") + fmt.Println(err) + return + } + fileSize, err := fileLen(os.Args[1]) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(fileSize) +} diff --git a/ch5/2/go.mod b/ch5/2/go.mod new file mode 100644 index 0000000..a9fb6c8 --- /dev/null +++ b/ch5/2/go.mod @@ -0,0 +1,3 @@ +module ch5_2 + +go 1.25.0 diff --git a/ch5/3/ch5_3.exe b/ch5/3/ch5_3.exe new file mode 100644 index 0000000..02cf45d Binary files /dev/null and b/ch5/3/ch5_3.exe differ diff --git a/ch5/3/ch5_3.go b/ch5/3/ch5_3.go new file mode 100644 index 0000000..bcf94fa --- /dev/null +++ b/ch5/3/ch5_3.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func prefixer(prefix string) func(string) string { + return func(s string) string { return prefix + " " + s } +} + +func main() { + helloPrefix := prefixer("Hello") + fmt.Println(helloPrefix("Bob")) // should print Hello Bob + fmt.Println(helloPrefix("Maria")) // should print Hello Maria +} diff --git a/ch5/3/go.mod b/ch5/3/go.mod new file mode 100644 index 0000000..cfed642 --- /dev/null +++ b/ch5/3/go.mod @@ -0,0 +1,3 @@ +module ch5_3 + +go 1.25.0 diff --git a/ch6/1/ch6_1.exe b/ch6/1/ch6_1.exe new file mode 100644 index 0000000..fa5e680 Binary files /dev/null and b/ch6/1/ch6_1.exe differ diff --git a/ch6/1/ch6_1.go b/ch6/1/ch6_1.go new file mode 100644 index 0000000..bf17c55 --- /dev/null +++ b/ch6/1/ch6_1.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +type Person struct { + FirstName string + LastName string + Age int +} + +func MakePerson(firstName, lastName string, age int) Person { + return Person{firstName, lastName, age} +} + +func MakePersonPointer(firstName, lastName string, age int) *Person { + return &Person{firstName, lastName, age} +} + +func main() { + fmt.Println(MakePerson("Ivan", "Ivanov", 21)) + fmt.Println(*MakePersonPointer("Peter", "Petrov", 22)) +} diff --git a/ch6/1/go.mod b/ch6/1/go.mod new file mode 100644 index 0000000..33f8c0b --- /dev/null +++ b/ch6/1/go.mod @@ -0,0 +1,3 @@ +module ch6_1 + +go 1.25.0 diff --git a/ch6/2/ch6_2.exe b/ch6/2/ch6_2.exe new file mode 100644 index 0000000..fa74b41 Binary files /dev/null and b/ch6/2/ch6_2.exe differ diff --git a/ch6/2/ch6_2.go b/ch6/2/ch6_2.go new file mode 100644 index 0000000..29866ec --- /dev/null +++ b/ch6/2/ch6_2.go @@ -0,0 +1,30 @@ +package main + +import "fmt" + +func UpdateSlice(strSlice []string, cmpStr string) { + sliceSize := len(strSlice) + for i := 0; i < sliceSize; i++ { + if cmpStr == strSlice[i] { + strSlice = strSlice[:i] + break + } + } + fmt.Println(strSlice) +} + +func GrowSlice(strSlice []string, appStr string) { + strSlice = append(strSlice, appStr) + fmt.Println(strSlice) +} + +func main() { + strSlice := []string{"aaa", "bbb", "ccc", "ddd"} + fmt.Println("Before function UpdateSlice: ", strSlice) + UpdateSlice(strSlice, "ccc") + fmt.Println("After function UpdateSlice: ", strSlice) + strSlice = []string{"aaa", "bbb", "ccc", "ddd"} + fmt.Println("Before function GrowSlice: ", strSlice) + GrowSlice(strSlice, "eee") + fmt.Println("After function GrowSlice: ", strSlice) +} diff --git a/ch6/2/go.mod b/ch6/2/go.mod new file mode 100644 index 0000000..5b35879 --- /dev/null +++ b/ch6/2/go.mod @@ -0,0 +1,3 @@ +module ch6_2 + +go 1.25.0 diff --git a/ch6/3/ch6_3.exe b/ch6/3/ch6_3.exe new file mode 100644 index 0000000..dd70cad Binary files /dev/null and b/ch6/3/ch6_3.exe differ diff --git a/ch6/3/ch6_3.go b/ch6/3/ch6_3.go new file mode 100644 index 0000000..db7966d --- /dev/null +++ b/ch6/3/ch6_3.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +type Person struct { + name string + age int +} + +func MakePerson(name string, age int) Person { + return Person{name, age} +} + +func main() { + personSlice := make([]Person, 0, 512) + for i := 0; i < 10_000_000; i++ { + personSlice = append(personSlice, MakePerson("aaa", i)) + } + fmt.Println(personSlice[:100]) +} diff --git a/ch6/3/go.mod b/ch6/3/go.mod new file mode 100644 index 0000000..a6ec051 --- /dev/null +++ b/ch6/3/go.mod @@ -0,0 +1,3 @@ +module ch6_3 + +go 1.25.0 diff --git a/ch7/1/ch7_1.go b/ch7/1/ch7_1.go new file mode 100644 index 0000000..809707e --- /dev/null +++ b/ch7/1/ch7_1.go @@ -0,0 +1,16 @@ +package main + +type Team struct { + Name string + Players []string +} + +type League struct { + name string + Teams map[string]Team + Wins map[string]int +} + +func main() { + +} diff --git a/ch7/1/go.mod b/ch7/1/go.mod new file mode 100644 index 0000000..8788176 --- /dev/null +++ b/ch7/1/go.mod @@ -0,0 +1,3 @@ +module ch7_1 + +go 1.25.0 diff --git a/ch7/2/ch7_2.exe b/ch7/2/ch7_2.exe new file mode 100644 index 0000000..26c147b Binary files /dev/null and b/ch7/2/ch7_2.exe differ diff --git a/ch7/2/ch7_2.go b/ch7/2/ch7_2.go new file mode 100644 index 0000000..815a058 --- /dev/null +++ b/ch7/2/ch7_2.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "sort" +) + +type Team struct { + Name string + Players []string +} + +type League struct { + name string + Teams map[string]Team + Wins map[string]int +} + +func (l *League) MatchResult(firstTeam string, firstScore int, secondTeam string, secondScore int) { + if _, ok := l.Teams[firstTeam]; ok { + return + } + if _, ok := l.Teams[secondTeam]; ok { + return + } + if firstScore == secondScore { + return + } + if firstScore > secondScore { + l.Wins[firstTeam]++ + } else if secondScore > firstScore { + l.Wins[secondTeam]++ + } +} + +func (l League) Ranking() []string { + teamRanking := make([]string, 0, len(l.Wins)) + for winsK := range l.Wins { + teamRanking = append(teamRanking, winsK) + } + sort.Slice(teamRanking, func(i, j int) bool { + return l.Wins[teamRanking[i]] < l.Wins[teamRanking[j]] + }) + return teamRanking +} + +func main() { + league := League{"newLeague", map[string]Team{}, map[string]int{"first": 1, "second": 2, "third": 6, "fourth": 3, "fifth": 4}} + league.MatchResult("third", 5, "second", 4) + fmt.Println(league) + fmt.Println(league.Ranking()) +} diff --git a/ch7/2/go.mod b/ch7/2/go.mod new file mode 100644 index 0000000..2309e66 --- /dev/null +++ b/ch7/2/go.mod @@ -0,0 +1,3 @@ +module ch7_2 + +go 1.25.0 diff --git a/ch7/3/ch7_3.exe b/ch7/3/ch7_3.exe new file mode 100644 index 0000000..213aac4 Binary files /dev/null and b/ch7/3/ch7_3.exe differ diff --git a/ch7/3/ch7_3.go b/ch7/3/ch7_3.go new file mode 100644 index 0000000..b9a7253 --- /dev/null +++ b/ch7/3/ch7_3.go @@ -0,0 +1,81 @@ +package main + +import ( + "fmt" + "io" + "sort" +) + +type Ranker interface { + Ranking() []string +} + +type testWriter struct{} + +func (p testWriter) Write(bs []byte) (int, error) { + if len(bs) == 0 { + return 0, nil + } + for i := 0; i < len(bs); i++ { + fmt.Print(string(bs[i])) + } + return len(bs), nil +} + +func RankPrinter(ranker Ranker, writer io.Writer) { + rankSize := len(ranker.Ranking()) + for i, teams := range ranker.Ranking() { + writer.Write([]byte(teams)) + if i != rankSize-1 { + writer.Write([]byte("\n")) + } + } +} + +type Team struct { + Name string + Players []string +} + +type League struct { + name string + Teams map[string]Team + Wins map[string]int +} + +func (l *League) MatchResult(firstTeam string, firstScore int, secondTeam string, secondScore int) { + if _, ok := l.Teams[firstTeam]; ok { + return + } + if _, ok := l.Teams[secondTeam]; ok { + return + } + if firstScore == secondScore { + return + } + if firstScore > secondScore { + l.Wins[firstTeam]++ + } else if secondScore > firstScore { + l.Wins[secondTeam]++ + } +} + +func (l League) Ranking() []string { + teamRanking := make([]string, 0, len(l.Wins)) + for winsK := range l.Wins { + teamRanking = append(teamRanking, winsK) + } + sort.Slice(teamRanking, func(i, j int) bool { + return l.Wins[teamRanking[i]] < l.Wins[teamRanking[j]] + }) + return teamRanking +} + +func main() { + league := League{"newLeague", map[string]Team{}, map[string]int{"first": 1, "second": 2, "third": 6, "fourth": 3, "fifth": 4}} + league.MatchResult("third", 5, "second", 4) + fmt.Println(league) + fmt.Println(league.Ranking()) + writer := testWriter{} + RankPrinter(league, writer) +} diff --git a/ch7/3/go.mod b/ch7/3/go.mod new file mode 100644 index 0000000..9d8b47d --- /dev/null +++ b/ch7/3/go.mod @@ -0,0 +1,3 @@ +module ch7_3 + +go 1.25.0 diff --git a/ch8/1/ch8_1.exe b/ch8/1/ch8_1.exe new file mode 100644 index 0000000..f1a8687 Binary files /dev/null and b/ch8/1/ch8_1.exe differ diff --git a/ch8/1/ch8_1.go b/ch8/1/ch8_1.go new file mode 100644 index 0000000..d5de10a --- /dev/null +++ b/ch8/1/ch8_1.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +type Number interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | + ~uint32 | ~uint64 | ~float32 | ~float64 +} + +func NumDoubler[T Number](val T) T { + return val * 2 +} + +func main() { + val := 10.3 + fmt.Println(val) + val = NumDoubler(val) + fmt.Println(val) + val = NumDoubler(val) + fmt.Println(val) + val = NumDoubler(val) + fmt.Println(val) + val = NumDoubler(val) + fmt.Println(val) + val = NumDoubler(val) + fmt.Println(val) +} diff --git a/ch8/1/go.mod b/ch8/1/go.mod new file mode 100644 index 0000000..36776cd --- /dev/null +++ b/ch8/1/go.mod @@ -0,0 +1,3 @@ +module ch8_1 + +go 1.25.0 diff --git a/ch8/2/ch8_2.exe b/ch8/2/ch8_2.exe new file mode 100644 index 0000000..d168b7f Binary files /dev/null and b/ch8/2/ch8_2.exe differ diff --git a/ch8/2/ch8_2.go b/ch8/2/ch8_2.go new file mode 100644 index 0000000..b35ab56 --- /dev/null +++ b/ch8/2/ch8_2.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "strconv" +) + +type Printable interface { + Number | NumberFloat + fmt.Stringer +} + +type Number struct { + val int +} + +func (num Number) String() string { + return string(strconv.Itoa(num.val)) +} + +type NumberFloat struct { + val float64 +} + +func (num NumberFloat) String() string { + return string(strconv.Itoa(int(num.val))) +} + +func Print[T Printable](num T) { + fmt.Println(num) +} + +func main() { + firstTest := Number{val: 5} + secondTest := NumberFloat{val: 7.2} + Print(firstTest) + Print(secondTest) +} diff --git a/ch8/2/go.mod b/ch8/2/go.mod new file mode 100644 index 0000000..bbd9b26 --- /dev/null +++ b/ch8/2/go.mod @@ -0,0 +1,3 @@ +module ch8_2 + +go 1.25.0 diff --git a/ch8/3/ch8_3.exe b/ch8/3/ch8_3.exe new file mode 100644 index 0000000..6b9f735 Binary files /dev/null and b/ch8/3/ch8_3.exe differ diff --git a/ch8/3/ch8_3.go b/ch8/3/ch8_3.go new file mode 100644 index 0000000..4a85f06 --- /dev/null +++ b/ch8/3/ch8_3.go @@ -0,0 +1,91 @@ +package main + +import "fmt" + +type Node[T comparable] struct { + val T + next *Node[T] +} + +func (elem *Node[T]) Add(newVal T) { + currPos := elem.GoToEnd() + currPos.next = &Node[T]{val: newVal, next: nil} +} + +func (elem *Node[T]) GoToEnd() *Node[T] { + currPos := elem + for currPos.next != nil { + currPos = currPos.next + } + return currPos +} + +func (elem *Node[T]) GoToPosition(len int) *Node[T] { + currPos := elem + index := 0 + for currPos.next != nil && index != len { + currPos = currPos.next + index++ + } + return currPos +} + +func (elem *Node[T]) Insert(newVal T, index int) { + currPos := elem.GoToPosition(index - 1) + nextNode := currPos.next + currPos.next = &Node[T]{val: newVal, next: nextNode} +} + +func (elem Node[T]) Index(cmpVal T) int { + currPos := &elem + index := 0 + for currPos != nil && currPos.val != cmpVal { + currPos = currPos.next + index++ + } + if currPos == nil { + return -1 + } + return index +} + +func (elem Node[T]) GoToEndWithOutput() { + currPos := elem + for currPos.next != nil { + fmt.Println(currPos) + currPos = *currPos.next + } + fmt.Println(currPos) +} + +func main() { + linkedList := Node[int]{val: 12} + linkedList.Add(4) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Add(7) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Add(22) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Add(65) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Add(2) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Add(3) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Add(1) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Insert(10, 4) + linkedList.GoToEndWithOutput() + fmt.Println() + linkedList.Insert(11, 5) + linkedList.GoToEndWithOutput() + fmt.Println() + fmt.Println(linkedList.Index(11)) +} diff --git a/ch8/3/go.mod b/ch8/3/go.mod new file mode 100644 index 0000000..6259ba9 --- /dev/null +++ b/ch8/3/go.mod @@ -0,0 +1,3 @@ +module ch8_3 + +go 1.25.0 diff --git a/ch9/1/ch9_1.exe b/ch9/1/ch9_1.exe new file mode 100644 index 0000000..ffec0e4 Binary files /dev/null and b/ch9/1/ch9_1.exe differ diff --git a/ch9/1/ch9_1.go b/ch9/1/ch9_1.go new file mode 100644 index 0000000..f5f6dc7 --- /dev/null +++ b/ch9/1/ch9_1.go @@ -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)) +} diff --git a/ch9/1/go.mod b/ch9/1/go.mod new file mode 100644 index 0000000..48f206e --- /dev/null +++ b/ch9/1/go.mod @@ -0,0 +1,3 @@ +module ch9_1 + +go 1.25.0 diff --git a/ch9/2/ch9_2.exe b/ch9/2/ch9_2.exe new file mode 100644 index 0000000..654e85c Binary files /dev/null and b/ch9/2/ch9_2.exe differ diff --git a/ch9/2/ch9_2.go b/ch9/2/ch9_2.go new file mode 100644 index 0000000..b9bd4a0 --- /dev/null +++ b/ch9/2/ch9_2.go @@ -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") + } +} diff --git a/ch9/2/go.mod b/ch9/2/go.mod new file mode 100644 index 0000000..0d4bc72 --- /dev/null +++ b/ch9/2/go.mod @@ -0,0 +1,3 @@ +module ch9_2 + +go 1.25.0