push all exercices
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user