Add lesson 1: Go in one afternoon
3 changed files with 42 additions and 0 deletions
lessons/01-go-in-one-afternoon/README.md ADDED
| @@ -0,0 +1,19 @@ | ||
| 1 | +# Lesson 1: Go in one afternoon | |
| 2 | + | |
| 3 | +We look at go in one afternoon the way working developers use it, with examples you can change and run right away. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/go-apis/lessons/1](https://glibcode.net/courses/go-apis/lessons/1) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `main.go`: variables, slices, maps and a function in Go | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +go run ./lessons/01-go-in-one-afternoon | |
| 15 | +``` | |
| 16 | + | |
| 17 | +## Exercises | |
| 18 | + | |
| 19 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/01-go-in-one-afternoon/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 1 | |
| 2 | + | |
| 3 | +1. Make the example ask for its input instead of using a fixed value. | |
| 4 | +2. Combine this lesson with the previous one: use something from both in a small program of your own. | |
| 5 | +3. Look up one function or command the example uses in the official documentation. Note one option you didn't know. | |
lessons/01-go-in-one-afternoon/main.go ADDED
| @@ -0,0 +1,18 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +import "fmt" | |
| 4 | + | |
| 5 | +func average(xs []float64) float64 { | |
| 6 | + sum := 0.0 | |
| 7 | + for _, x := range xs { | |
| 8 | + sum += x | |
| 9 | + } | |
| 10 | + return sum / float64(len(xs)) | |
| 11 | +} | |
| 12 | + | |
| 13 | +func main() { | |
| 14 | + scores := map[string][]float64{"ana": {7, 9}, "ben": {5, 8, 10}} | |
| 15 | + for name, s := range scores { | |
| 16 | + fmt.Printf("%s: %.1f\n", name, average(s)) | |
| 17 | + } | |
| 18 | +} | |