From 2c05f038da1384c38cc1a97a305da441533d29c7 Mon Sep 17 00:00:00 2001 From: Dave Dietrick Date: Sat, 30 Nov 2024 10:43:05 +0100 Subject: [PATCH] Added more go pages --- docs/go/advanced/testing.md | 59 ++++++++++++++++++++++++++++++++++++ docs/go/basics/collection.md | 4 +++ docs/go/basics/packages.md | 49 ++++++++++++++++++++++++++++++ docs/go/basics/struct.md | 2 +- docs/go/sidebar.json | 4 +++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 docs/go/advanced/testing.md diff --git a/docs/go/advanced/testing.md b/docs/go/advanced/testing.md new file mode 100644 index 0000000..c15851d --- /dev/null +++ b/docs/go/advanced/testing.md @@ -0,0 +1,59 @@ +# Testing + +Test files in go are kept alongside the source code with postfix `_test.go`. Functions beginning with `Test*` that take an argument `(t *testing.T)` will be run as unit tests. A test fails if a condition leading to `t.Error` or `t.Fail` is met. + +```bash +# Test everything in current directory +go test . + +# Test everything in subdirectories +go test ./... + +# See coverage percentage +go test -cover . + +# Generate coverage file +go test -coverprofile=coverage.out . + +# View code coverage from file +go tool cover -html=coverage.out + +# Run individual test +go test -run Test_alpha_isPrime + +# Run groups of tests +go test -run Test_alpha +``` + +## Test cases + +Within a test function, you can run multiple related test cases by iterating over a slice of cases with `t.Run`. + +```go +func Test_alpha_isPrime(t *testing.T) { + primeTests := []struct { + name string + num int + expected bool + message string + }{ + {"prime", 7, true, "7 is prime!"}, + {"not prime", 8, false, "8 is not prime, it is divisible by 2"}, + {"zero", 0, false, "0 is not prime, by definition!"}, + {"one", 1, false, "1 is not prime, by definition!"}, + {"negative", -1, false, "Negative numbers are not prime, by definition!"}, + } + + for _, tt := range primeTests { + t.Run(tt.name, func(t *testing.T) { + actual, msg := isPrime(tt.num) + if actual != tt.expected { + t.Errorf("isPrime(%d): expected %t, actual %t", tt.num, tt.expected, actual) + } + if msg != tt.message { + t.Errorf("isPrime(%d): expected %s, actual %s", tt.num, tt.message, msg) + } + }) + } +} +``` diff --git a/docs/go/basics/collection.md b/docs/go/basics/collection.md index 0fa441e..9ebabb6 100644 --- a/docs/go/basics/collection.md +++ b/docs/go/basics/collection.md @@ -1,5 +1,9 @@ # Collections +## Make + +The `make` keyword is used to create slices, maps, and channels that are initialized and ready for use. For example, a slice is a three component object with a pointer to an array, a length, and a capacity. Until these are set, the slice is considered nil. The `make` keyword initialized these variables and makes the slice ready to use. It also returns value of type T instead of a pointer with `new`. + ## Arrays Arrays in Go are fixed length and must be declared at initialization. diff --git a/docs/go/basics/packages.md b/docs/go/basics/packages.md index 4772f43..2c0ff49 100644 --- a/docs/go/basics/packages.md +++ b/docs/go/basics/packages.md @@ -17,3 +17,52 @@ func main() { } ``` + +## Projects + +Projects can be broken up into subpackages so they can be imported separately. The `internal` directoy can not be imported by someone using your package. Binaries should be kept in the `cmd` folder. Within `cmd` all files and any other binary files should include `package main`. In subpackages like `auth`, `token`, `hash`, and `trace` should have their respective package names. + +``` +project-root-directory/ + go.mod + modname.go + modname_test.go + auth/ + auth.go + auth_test.go + token/ + token.go + token_test.go + hash/ + hash.go + internal/ + trace/ + trace.go + cmd/ + prog1/ + main.go + prog2/ + main.go +``` + +`go.mod` includes the module name or where the code is located. For example, if the code is kept at `github.com/someuser/modname`, then the file should include: + +``` +module github.com/someuser/modname +``` + +In the importing code: + +```go +import "github.com/someuser/modname" +import "github.com/someuser/modname/auth" +import "github.com/someuser/modname/auth/token" +import "github.com/someuser/modname/hash" +``` + +To install the binaries: + +```bash +$ go install github.com/someuser/modname/prog1@latest +$ go install github.com/someuser/modname/prog2@latest +``` diff --git a/docs/go/basics/struct.md b/docs/go/basics/struct.md index 8b66124..1a139e4 100644 --- a/docs/go/basics/struct.md +++ b/docs/go/basics/struct.md @@ -1,6 +1,6 @@ # Structs -Structs are collections of fields. They are defined as types and then created using brackets to declare the values. +Structs are collections of fields. They are defined as types and then created using brackets to declare the values. You can also create structs with the `new` keyword which creates a pointer of type T and initializes the object with zero values. ```go import ( diff --git a/docs/go/sidebar.json b/docs/go/sidebar.json index 0c5f929..c093a4f 100644 --- a/docs/go/sidebar.json +++ b/docs/go/sidebar.json @@ -12,5 +12,9 @@ { "text": "Structs", "link": "/go/basics/struct" }, { "text": "Concurrency", "link": "/go/basics/concurrency" } ] + }, + { + "text": "Go Advanced", + "items": [{ "text": "Testing", "link": "/go/advanced/testing" }] } ]