push all exercices
This commit is contained in:
15
ch1/Makefile
Normal file
15
ch1/Makefile
Normal file
@@ -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
|
||||||
3
ch1/go.mod
Normal file
3
ch1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module hello_world
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
9
ch1/hello.go
Normal file
9
ch1/hello.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
fmt.Printf("Hello ttt sad , %s!\n", "world")
|
||||||
|
|
||||||
|
}
|
||||||
BIN
ch1/hello_world.exe
Normal file
BIN
ch1/hello_world.exe
Normal file
Binary file not shown.
BIN
ch2/1/ch2.exe
Normal file
BIN
ch2/1/ch2.exe
Normal file
Binary file not shown.
10
ch2/1/const_declarations.go
Normal file
10
ch2/1/const_declarations.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch2/1/go.mod
Normal file
3
ch2/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch2/2/ch2_2.exe
Normal file
BIN
ch2/2/ch2_2.exe
Normal file
Binary file not shown.
11
ch2/2/ch2_2.go
Normal file
11
ch2/2/ch2_2.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch2/2/go.mod
Normal file
3
ch2/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch2_2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch2/3/ch2_3.exe
Normal file
BIN
ch2/3/ch2_3.exe
Normal file
Binary file not shown.
15
ch2/3/ch3_2.go
Normal file
15
ch2/3/ch3_2.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch2/3/go.mod
Normal file
3
ch2/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch2_3
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch3/1/ch3_1.exe
Normal file
BIN
ch3/1/ch3_1.exe
Normal file
Binary file not shown.
13
ch3/1/ch3_1.go
Normal file
13
ch3/1/ch3_1.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch3/1/go.mod
Normal file
3
ch3/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch3_1
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch3/2/ch3_2.exe
Normal file
BIN
ch3/2/ch3_2.exe
Normal file
Binary file not shown.
9
ch3/2/ch3_2.go
Normal file
9
ch3/2/ch3_2.go
Normal file
@@ -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]))
|
||||||
|
}
|
||||||
3
ch3/2/go.mod
Normal file
3
ch3/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch3_2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch3/3/ch3_3.exe
Normal file
BIN
ch3/3/ch3_3.exe
Normal file
Binary file not shown.
29
ch3/3/ch3_3.go
Normal file
29
ch3/3/ch3_3.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch3/3/go.mod
Normal file
3
ch3/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch3_3
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch4/1/ch4_1.exe
Normal file
BIN
ch4/1/ch4_1.exe
Normal file
Binary file not shown.
14
ch4/1/ch4_1.go
Normal file
14
ch4/1/ch4_1.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch4/1/go.mod
Normal file
3
ch4/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch4_1
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch4/2/ch4_2.exe
Normal file
BIN
ch4/2/ch4_2.exe
Normal file
Binary file not shown.
26
ch4/2/ch4_2.go
Normal file
26
ch4/2/ch4_2.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch4/2/go.mod
Normal file
3
ch4/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch4_2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch4/3/ch4_3.exe
Normal file
BIN
ch4/3/ch4_3.exe
Normal file
Binary file not shown.
12
ch4/3/ch4_3.go
Normal file
12
ch4/3/ch4_3.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch4/3/go.mod
Normal file
3
ch4/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch4_3
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch5/1/ch5_1.exe
Normal file
BIN
ch5/1/ch5_1.exe
Normal file
Binary file not shown.
20
ch5/1/ch5_1.go
Normal file
20
ch5/1/ch5_1.go
Normal file
@@ -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))
|
||||||
|
}
|
||||||
3
ch5/1/go.mod
Normal file
3
ch5/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch5_1
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch5/2/ch5_2.exe
Normal file
BIN
ch5/2/ch5_2.exe
Normal file
Binary file not shown.
44
ch5/2/ch5_2.go
Normal file
44
ch5/2/ch5_2.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch5/2/go.mod
Normal file
3
ch5/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch5_2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch5/3/ch5_3.exe
Normal file
BIN
ch5/3/ch5_3.exe
Normal file
Binary file not shown.
13
ch5/3/ch5_3.go
Normal file
13
ch5/3/ch5_3.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
3
ch5/3/go.mod
Normal file
3
ch5/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch5_3
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch6/1/ch6_1.exe
Normal file
BIN
ch6/1/ch6_1.exe
Normal file
Binary file not shown.
22
ch6/1/ch6_1.go
Normal file
22
ch6/1/ch6_1.go
Normal file
@@ -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))
|
||||||
|
}
|
||||||
3
ch6/1/go.mod
Normal file
3
ch6/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch6_1
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch6/2/ch6_2.exe
Normal file
BIN
ch6/2/ch6_2.exe
Normal file
Binary file not shown.
30
ch6/2/ch6_2.go
Normal file
30
ch6/2/ch6_2.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch6/2/go.mod
Normal file
3
ch6/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch6_2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch6/3/ch6_3.exe
Normal file
BIN
ch6/3/ch6_3.exe
Normal file
Binary file not shown.
20
ch6/3/ch6_3.go
Normal file
20
ch6/3/ch6_3.go
Normal file
@@ -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])
|
||||||
|
}
|
||||||
3
ch6/3/go.mod
Normal file
3
ch6/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch6_3
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
16
ch7/1/ch7_1.go
Normal file
16
ch7/1/ch7_1.go
Normal file
@@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
3
ch7/1/go.mod
Normal file
3
ch7/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch7_1
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch7/2/ch7_2.exe
Normal file
BIN
ch7/2/ch7_2.exe
Normal file
Binary file not shown.
52
ch7/2/ch7_2.go
Normal file
52
ch7/2/ch7_2.go
Normal file
@@ -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())
|
||||||
|
}
|
||||||
3
ch7/2/go.mod
Normal file
3
ch7/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch7_2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch7/3/ch7_3.exe
Normal file
BIN
ch7/3/ch7_3.exe
Normal file
Binary file not shown.
81
ch7/3/ch7_3.go
Normal file
81
ch7/3/ch7_3.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch7/3/go.mod
Normal file
3
ch7/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch7_3
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch8/1/ch8_1.exe
Normal file
BIN
ch8/1/ch8_1.exe
Normal file
Binary file not shown.
27
ch8/1/ch8_1.go
Normal file
27
ch8/1/ch8_1.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch8/1/go.mod
Normal file
3
ch8/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch8_1
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch8/2/ch8_2.exe
Normal file
BIN
ch8/2/ch8_2.exe
Normal file
Binary file not shown.
38
ch8/2/ch8_2.go
Normal file
38
ch8/2/ch8_2.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
3
ch8/2/go.mod
Normal file
3
ch8/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch8_2
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch8/3/ch8_3.exe
Normal file
BIN
ch8/3/ch8_3.exe
Normal file
Binary file not shown.
91
ch8/3/ch8_3.go
Normal file
91
ch8/3/ch8_3.go
Normal file
@@ -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))
|
||||||
|
}
|
||||||
3
ch8/3/go.mod
Normal file
3
ch8/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch8_3
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
BIN
ch9/1/ch9_1.exe
Normal file
BIN
ch9/1/ch9_1.exe
Normal file
Binary file not shown.
33
ch9/1/ch9_1.go
Normal file
33
ch9/1/ch9_1.go
Normal 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
3
ch9/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ch9_1
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
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