Skip to content

Commit

Permalink
chapter 08 finished;
Browse files Browse the repository at this point in the history
 chapter 09 initialized
  • Loading branch information
MKaczkow committed Mar 5, 2024
1 parent 88558d8 commit 19c1708
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 1 deletion.
21 changes: 21 additions & 0 deletions learning_go/08_errors/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.DEFAULT_GOAL := run

fmt:
go fmt ./...
.PHONY: fmt

lint: fmt
golint ./...
.PHONY: lint

vet: fmt
go vet ./...
.PHONY: vet

build: vet
go build panic_recover_example.go
.PHONY: build

run: vet
go run panic_recover_example.go
.PHONY: run
13 changes: 13 additions & 0 deletions learning_go/08_errors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@
#### Sentinel errors
* should be treated as `read-only` values
* shouldn't be modified or created

#### Errors are values
* `error` is an interface
* thus, you can implement your own error
* for interface to be equal to `nil` (`== nil`), both base type and value should be `nil`
* most common way is to return `nil` as error's value if no error occurred

#### Wrapping errors
* sequence of errors, wrapped is called `error chain`
* `errors.Is` and `errors.As` are used to work with error chains

#### Panic and recover
* shouldn't be overused
25 changes: 25 additions & 0 deletions learning_go/08_errors/errors_is_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"errors"
"fmt"
"os"
)

func fileChecker(name string) error {
f, err := os.Open(name)
if err != nil {
return fmt.Errorf("in fileChecker: %w", err)
}
f.Close()
return nil
}

func bckp_main() {
err := fileChecker("not_here.txt")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
fmt.Println("That file doesn't exist")
}
}
}
3 changes: 3 additions & 0 deletions learning_go/08_errors/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module main

go 1.20
11 changes: 11 additions & 0 deletions learning_go/08_errors/panic_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "os"

func doPanic(msg string) {
panic(msg)
}

func bis_bckp_main() {
doPanic(os.Args[0])
}
16 changes: 16 additions & 0 deletions learning_go/08_errors/panic_recover_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

func div60(i int) {
defer func() {
if v := recover(); v != nil {
println("recovered:", v)
}
}()
println(60 / i)
}

func main() {
for _, val := range []int{1, 2, 0, 3} {
div60(val)
}
}
18 changes: 18 additions & 0 deletions learning_go/09_modules_packages_imports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 09_modules_packages_imports

### Intro
* repositories
* modules
* packages
* 1 or more packages per module
* 1 module per 1 repository

### Modules
* `go.mod` file is necessary (and needs to be correct of course)
* `go.mod` file contains:
- module name
- go version
- dependencies
- version of dependencies
- (optional) replace
- (optional) exclude
2 changes: 1 addition & 1 deletion learning_go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Directory for studying 'experiment', learning from papercover book "Język Go. T
- [x] chapter 05 - functions
- [x] chapter 06 - pointers
- [x] chapter 07 - types, methods and interfaces
- [ ] chapter 08 - errors
- [x] chapter 08 - errors
- [ ] chapter 09 - modules, packages and imports
- [ ] chapter 10 - concurrency in Go
- [ ] chapter 11 - standard library
Expand Down

0 comments on commit 19c1708

Please sign in to comment.