Skip to content

Commit 2244e7e

Browse files
Project introducing stdout/stderr and error handling (#116)
There is a lot of "teaching" material in this project, and I'd love to cite some external references rather than writing new material, but I couldn't find any particularly accessible (and not-very-language-specific) introductions to the concepts... Co-authored-by: Sally McGrath <[email protected]>
1 parent da6e40a commit 2244e7e

File tree

14 files changed

+602
-29
lines changed

14 files changed

+602
-29
lines changed

prep/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ It doesn't matter what this does: what's important is the input command and the
8080
Input command:
8181

8282
```console
83-
curl -i 'http://localhost:8080/'
83+
> curl -i 'http://localhost:8080/'
8484
```
8585

8686
Expected output:

projects/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Each project has its own directory with a README.md file that has instructions.
1818

1919
Most exercises finish with a list of optional extension tasks. It's highly recommended that you try them out. Note that often the extensions are open-ended and under-specified - make sure to think about them with a curious mind: Why are they useful? What trade-offs do they have?
2020

21+
1. [Output and Error Handling](./output-and-error-handling)
22+
<br>An introduction to how to handle errors in Go, and how to present information to users of programs run on the command line.
2123
1. [CLI & Files](./cli-files)
2224
<br>An introduction to building things with Go by replicating the unix tools `cat` and `ls`.
2325
1. [File Parsing](./file-parsing)

projects/file-parsing/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The format is as follows:
5555
The tool `od` (which you can learn more about [here](https://man7.org/linux/man-pages/man1/od.1.html)) can be useful for exploring binary data. For instance, we can run:
5656

5757
```console
58-
% od -t x1 projects/file-parsing/examples/custom-binary-le.bin
58+
> od -t x1 projects/file-parsing/examples/custom-binary-le.bin
5959
0000000 ff fe 0a 00 00 00 41 79 61 00 1e 00 00 00 50 72
6060
0000020 69 73 68 61 00 ff ff ff ff 43 68 61 72 6c 69 65
6161
0000040 00 19 00 00 00 4d 61 72 67 6f 74 00
@@ -99,21 +99,21 @@ Two examples of this are [`jq`](https://stedolan.github.io/jq/) (which allows yo
9999
For example, you can use `jq` to answer the question "Who had the highest score" without needing to write a whole program:
100100

101101
```console
102-
% jq -r '. | max_by(.high_score).name' file-parsing/examples/json.txt
102+
> jq -r '. | max_by(.high_score).name' file-parsing/examples/json.txt
103103
Prisha
104104
```
105105

106106
Or use `fx` to do the same, but using more familiar JavaScript as the query language:
107107

108108
```console
109-
% fx file-parsing/examples/json.txt '.sort((l, r) => r.high_score - l.high_score)[0].name'
109+
> fx file-parsing/examples/json.txt '.sort((l, r) => r.high_score - l.high_score)[0].name'
110110
Prisha
111111
```
112112

113113
Similarly, a program called [`csvq`](https://github.com/mithrandie/csvq) can be used to query CSV files in a SQL-like query language:
114114

115115
```console
116-
# cat examples/data.csv | csvq 'SELECT * ORDER BY `high score` DESC LIMIT 1'
116+
> cat examples/data.csv | csvq 'SELECT * ORDER BY `high score` DESC LIMIT 1'
117117
+--------+------------+
118118
| name | high score |
119119
+--------+------------+

projects/metadata.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"order": [
3+
"output-and-error-handling",
34
"cli-files",
5+
"file-parsing",
46
"http-auth",
57
"server-database",
68
"multiple-servers",

projects/multiple-servers/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ There's a good, short guide to workloads on [scaleyourapp.com](https://scaleyour
7878

7979
Our file layout for this project will look like this:
8080

81-
```console
81+
```
8282
api/
8383
api.go
8484
assets/
@@ -108,12 +108,14 @@ Specifically, the `cmd/` files will import functionality from `api` and `static`
108108

109109
In reality, starting each will look like this:
110110

111+
API server:
111112
```console
112-
# api server
113-
$ DATABASE_URL='postgres://localhost:5432/go-server-database' go run ./cmd/api-server --port 8081
113+
> DATABASE_URL='postgres://localhost:5432/go-server-database' go run ./cmd/api-server --port 8081
114+
```
114115

115-
# static server
116-
$ go run ./cmd/static-server --path assets --port 8082
116+
Static server:
117+
```console
118+
> go run ./cmd/static-server --path assets --port 8082
117119
```
118120

119121
> 💡 See the [prep README.md](../prep/README.md#command-line-examples) for an explanation of this command line example.
@@ -328,7 +330,7 @@ import (
328330
The rest is up to you: hook this together and make this work:
329331

330332
```console
331-
$ go run ./cmd/static-server
333+
> go run ./cmd/static-server
332334
Hello!
333335
```
334336

@@ -339,14 +341,14 @@ To do that, add support for a command like flag: `--path` which will be where th
339341
Make this work:
340342

341343
```console
342-
$ go run ./cmd/static-server --path assets
344+
> go run ./cmd/static-server --path assets
343345
path: assets
344346
```
345347

346348
We also want this server to run on a specific port. Make this work:
347349

348350
```console
349-
$ go run ./cmd/static-server --path assets --port 8082
351+
> go run ./cmd/static-server --path assets --port 8082
350352
path: assets
351353
port: 8082
352354
```
@@ -364,7 +366,7 @@ It's possible to do this all in <20 lines of code.
364366
At the end, you should be able to run the server and visit [http://localhost:8082](http://localhost:8082) to see the image gallery!
365367

366368
```console
367-
$ go run ./cmd/static-server --path assets --port 8082
369+
> go run ./cmd/static-server --path assets --port 8082
368370
```
369371

370372
We aren't loading the list of images from an API yet; they're hard coded in the JavaScript. Making the API work is coming next.

0 commit comments

Comments
 (0)