mirror of
https://gitlab.com/djdietrick/docs
synced 2026-05-02 23:20:53 -04:00
Added more go pages
This commit is contained in:
59
docs/go/advanced/testing.md
Normal file
59
docs/go/advanced/testing.md
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
# Collections
|
# 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
|
||||||
|
|
||||||
Arrays in Go are fixed length and must be declared at initialization.
|
Arrays in Go are fixed length and must be declared at initialization.
|
||||||
|
|||||||
@@ -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
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Structs
|
# 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
|
```go
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -12,5 +12,9 @@
|
|||||||
{ "text": "Structs", "link": "/go/basics/struct" },
|
{ "text": "Structs", "link": "/go/basics/struct" },
|
||||||
{ "text": "Concurrency", "link": "/go/basics/concurrency" }
|
{ "text": "Concurrency", "link": "/go/basics/concurrency" }
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "Go Advanced",
|
||||||
|
"items": [{ "text": "Testing", "link": "/go/advanced/testing" }]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user