Skip to content

Commit 5b1b707

Browse files
Merge pull request #379 from ardanlabs/shorts2
Second batch of "shorts" recordings
2 parents 5b700e1 + e579a7d commit 5b1b707

File tree

132 files changed

+81405
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+81405
-1
lines changed

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ require (
88
github.com/patrickmn/go-cache v2.1.0+incompatible
99
github.com/pborman/uuid v1.2.1
1010
github.com/pkg/errors v0.9.1
11+
github.com/stretchr/testify v1.8.4
12+
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
13+
golang.org/x/text v0.10.0
1114
gonum.org/v1/plot v0.13.0
1215
)
1316

1417
require (
1518
git.sr.ht/~sbinet/gg v0.5.0 // indirect
1619
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b // indirect
1720
github.com/campoy/embedmd v1.0.0 // indirect
21+
github.com/davecgh/go-spew v1.1.1 // indirect
1822
github.com/go-fonts/liberation v0.3.1 // indirect
1923
github.com/go-latex/latex v0.0.0-20230307184459-12ec69307ad9 // indirect
2024
github.com/go-pdf/fpdf v0.8.0 // indirect
2125
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
2226
github.com/google/uuid v1.3.0 // indirect
2327
github.com/pmezard/go-difflib v1.0.0 // indirect
2428
golang.org/x/image v0.8.0 // indirect
25-
golang.org/x/text v0.10.0 // indirect
29+
gopkg.in/yaml.v3 v3.0.1 // indirect
2630
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/braintree/manners v0.0.0-20160418043613-82a8879fc5fd h1:ePesaBzdTmoMQ
1010
github.com/braintree/manners v0.0.0-20160418043613-82a8879fc5fd/go.mod h1:TNehV1AhBwtT7Bd+rh8G6MoGDbBLNs/sKdk3nvr4Yzg=
1111
github.com/campoy/embedmd v1.0.0 h1:V4kI2qTJJLf4J29RzI/MAt2c3Bl4dQSYPuflzwFH2hY=
1212
github.com/campoy/embedmd v1.0.0/go.mod h1:oxyr9RCiSXg0M3VJ3ks0UGfp98BpSSGr0kpiX3MzVl8=
13+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
14+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1315
github.com/go-fonts/dejavu v0.1.0 h1:JSajPXURYqpr+Cu8U9bt8K+XcACIHWqWrvWCKyeFmVQ=
1416
github.com/go-fonts/latin-modern v0.3.1 h1:/cT8A7uavYKvglYXvrdDw4oS5ZLkcOU22fa2HJ1/JVM=
1517
github.com/go-fonts/liberation v0.3.1 h1:9RPT2NhUpxQ7ukUvz3jeUckmN42T9D9TpjtQcqK/ceM=
@@ -34,13 +36,16 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3436
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3537
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3638
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
39+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
40+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
3741
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
3842
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
3943
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4044
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
4145
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
4246
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
4347
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea h1:vLCWI/yYrdEHyN2JzIzPO3aaQJHQdp89IZBA/+azVC4=
48+
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
4449
golang.org/x/image v0.8.0 h1:agUcRXV/+w6L9ryntYYsF2x9fQTMd4T8fiiYXAVW6Jg=
4550
golang.org/x/image v0.8.0/go.mod h1:PwLxp3opCYg4WR2WO9P0L6ESnsD6bLTWcw8zanLMVFM=
4651
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
@@ -85,5 +90,9 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
8590
gonum.org/v1/gonum v0.13.0 h1:a0T3bh+7fhRyqeNbiC3qVHYmkiQgit3wnNan/2c0HMM=
8691
gonum.org/v1/plot v0.13.0 h1:yb2Z/b8bY5h/xC4uix+ujJ+ixvPUvBmUOtM73CJzpsw=
8792
gonum.org/v1/plot v0.13.0/go.mod h1:mV4Bpu4PWTgN2CETURNF8hCMg7EtlZqJYCcmYo/t4Co=
93+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
94+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
95+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
96+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8897
honnef.co/go/tools v0.1.3/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
8998
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
type Client struct {
10+
baseURL string
11+
client http.Client
12+
}
13+
14+
func NewAPIClient(baseURL string) Client {
15+
return Client{baseURL: baseURL}
16+
}
17+
18+
func (c *Client) Health(ctx context.Context) error {
19+
url := fmt.Sprintf("%s/health", c.baseURL)
20+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
21+
if err != nil {
22+
return err
23+
}
24+
25+
resp, err := c.client.Do(req)
26+
if err != nil {
27+
return err
28+
}
29+
30+
if resp.StatusCode != http.StatusOK {
31+
return fmt.Errorf("%s", resp.Status)
32+
}
33+
34+
return nil
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
type ErrTransport struct{}
13+
14+
// implement http.RoundTripper
15+
func (e ErrTransport) RoundTrip(r *http.Request) (*http.Response, error) {
16+
return nil, fmt.Errorf("%s", "connection error")
17+
}
18+
19+
func TestHealthConnectionError(t *testing.T) {
20+
c := NewAPIClient("https://example.com")
21+
c.client.Transport = ErrTransport{}
22+
err := c.Health(context.Background())
23+
require.Error(t, err)
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"io/ioutil"
6+
"log"
7+
)
8+
9+
func main() {
10+
var config struct {
11+
Verbose bool
12+
}
13+
flag.BoolVar(&config.Verbose, "verbose", false, "be more verbose")
14+
flag.Parse()
15+
16+
if !config.Verbose {
17+
log.SetOutput(ioutil.Discard)
18+
}
19+
log.Printf("Please reinstall universe and reboot")
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"golang.org/x/text/language"
5+
"golang.org/x/text/message"
6+
)
7+
8+
func main() {
9+
fmt := message.NewPrinter(language.English)
10+
11+
symbol, price := "BRK-A", 506_466.10 // circa 2023-03-22
12+
fmt.Printf("%s is currently at $%.2f!\n", symbol, price)
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const (
8+
maxX = 1000
9+
maxY = 600
10+
)
11+
12+
type Point struct {
13+
X int
14+
Y int
15+
}
16+
17+
func inBounds(p Point) bool {
18+
return p.X >= 0 && p.X <= maxX && p.Y >= 0 && p.Y <= maxY
19+
}
20+
21+
func main() {
22+
ch := make(chan bool)
23+
points := []Point{
24+
{10, 20},
25+
{900, 700},
26+
{450, 353},
27+
}
28+
29+
// fan out
30+
for _, p := range points {
31+
p := p // avoid closure capture
32+
go func() {
33+
ch <- inBounds(p)
34+
}()
35+
}
36+
37+
// collect
38+
numOK := 0
39+
for range points {
40+
ok := <-ch
41+
if ok {
42+
numOK++
43+
}
44+
}
45+
fmt.Println(numOK, "points in range")
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Dummy DB package
2+
package main
3+
4+
import (
5+
"fmt"
6+
"time"
7+
)
8+
9+
type DB struct{}
10+
11+
func NewDB() *DB {
12+
return &DB{}
13+
}
14+
15+
func (d *DB) Query() *Rows {
16+
return &Rows{max: 5}
17+
}
18+
19+
type Rows struct {
20+
max int
21+
n int
22+
}
23+
24+
func (r *Rows) Next() bool {
25+
r.n++
26+
return r.n <= r.max
27+
}
28+
29+
func (r *Rows) Event() Event {
30+
t := time.Now().UTC().Add(-time.Duration((r.max-r.n-200)*527) * time.Millisecond)
31+
return Event{
32+
Time: t,
33+
User: "elliot",
34+
Action: "READ",
35+
URI: fmt.Sprintf("file:///reports/sec/%d.txt", r.n),
36+
}
37+
}
38+
39+
func (r *Rows) Err() error {
40+
return nil
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"log"
9+
"time"
10+
)
11+
12+
type Event struct {
13+
Time time.Time `json:"time"`
14+
User string `json:"user"`
15+
Action string `json:"action"`
16+
URI string `json:"uri"`
17+
}
18+
19+
func main() {
20+
db := NewDB()
21+
rows := db.Query()
22+
23+
var net bytes.Buffer
24+
25+
// Server
26+
enc := json.NewEncoder(&net)
27+
for rows.Next() {
28+
evt := rows.Event()
29+
enc.Encode(evt)
30+
}
31+
if err := rows.Err(); err != nil {
32+
log.Fatalf("error: %s", err)
33+
}
34+
35+
// Client
36+
dec := json.NewDecoder(&net)
37+
var e Event
38+
for {
39+
err := dec.Decode(&e)
40+
if err == io.EOF {
41+
break
42+
}
43+
if err != nil {
44+
log.Fatalf("error: %s", err)
45+
}
46+
fmt.Println(e)
47+
}
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
)
9+
10+
const (
11+
maxSize = 1 << 20 // 1MB
12+
)
13+
14+
func handler(w http.ResponseWriter, r *http.Request) {
15+
rdr := http.MaxBytesReader(w, r.Body, maxSize)
16+
defer rdr.Close()
17+
data, err := ioutil.ReadAll(rdr)
18+
if err != nil {
19+
http.Error(w, "can't read", http.StatusBadRequest)
20+
return
21+
}
22+
23+
fmt.Fprintf(w, "read %d bytes\n", len(data))
24+
}
25+
26+
func main() {
27+
http.HandleFunc("/", handler)
28+
log.Printf("INFO: max request size = %d", maxSize)
29+
30+
if err := http.ListenAndServe(":8080", nil); err != nil {
31+
log.Fatal(err)
32+
}
33+
}

0 commit comments

Comments
 (0)