Skip to content

Commit

Permalink
chapter 11 in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
MKaczkow authored Mar 21, 2024
1 parent 3e8587f commit 13454a2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions learning_go/11_standard_library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,45 @@
### I/O ops
* `io.Reader` and `io.Writer` interfaces
* `io.EOF` error, which is not true error
* interfaces are safe, type-wise (this weird combination of `duck typing` and `not-duck typing`)
* `io.Copy`, as name implies, copies data between `io.Reader` and `io.Writer`
* `io.Closer` is implemented by `types` like `os.File`, in cases, where you need to clean up something
* `io.Seeker` enables accesing data in random place of resource
* cool pattern `ioutil`, shows how to adding method to type, which is not really implementing them, to adhere to some interface (kinda like mocking)

```go
type nopCloser struct {
io.Reader
}

func (nopCloser) Close() error { return nil}

func NopCloser(r io.Reader) io.ReadCloser {
return nopCloser{r}
}
```

* but it breaks general rule of `don't return interfaces in functions`

### Time
* `time.Duration`
* `time.Hour`
* `time.Minute`
* ...
* nanosec is the smallest one
* `time.Time`
* timezone info is included, so don't compare using `==`, but using `Equals`
* a little weird format

```go
t, err := time.Parse("2006-02-01 15:04:05 -0700", "2016-13-03 00:00:00 +0000")
if err != nil {
return err
}
fmt.Println(t.Format("January 2, 2006 at 3:04:05PM MST"))
```
* now, time in Go guarantees monotonicity (uses monotonic clock, instead of real time)
* `time.Now()` used to rely on real time, which caused bug described on [cloudflare blog](https://blog.cloudflare.com/how-and-why-the-leap-second-affected-cloudflare-dns/)

### JSON

Expand Down

0 comments on commit 13454a2

Please sign in to comment.