Skip to content

go‐redis

andy-stark-redis edited this page Nov 27, 2024 · 1 revision

go-redis is the client library for Go.

Docs home https://redis.io/docs/latest/develop/clients/go-redis/
Github repo https://github.com/redis/go-redis
Doc examples branch master
Doc examples folder doctests
Usual suspect Vladyslav Vildanov (Github: vladvildanov)

go-redis uses Go's superb Testable examples system for doc testing. The filenames must end with _test.go to be detected by this and example function names start with the prefix Example (specifically, we tend to use the prefix ExampleClient_xxx in our docs examples, so it's probably best to stick with this). Go's testable examples work by comparing standard text output (from fmt.Println(), etc) against the content of comments in a special // Output: section which must be the last code in the function before the closing curly brace. For example, we have code like:

func ExampleClient_set_get() {
	...

	fmt.Println(res1) // >>> OK
	...
	fmt.Println(res2) // >>> Deimos
	...

	// Output:
	// OK
	// Deimos
}

VSCode is particularly recommended for Go examples, because if you have the Go extension installed, you will immediately see the testing "play button" in the sidebar as soon as you get your code correctly configured as a test (no explicit build or test scripts, etc).

The main issues with the testable examples are:

  • The Output: comment section must be the last thing in the function, so you can't add our directives like STEP_END after it.
  • Although the system is very convenient for most things, it can be a nuisance for non-deterministic output (time-dependent, etc) because the Println() calls are always part of the test.

Formatting

The Go extension uses the gopls Go language server which formats Go code in a standard way. Also, some formatting issues, such as unused variables and imports are actually errors in Go, so you'll catch them before the PR stage. Go doesn't complain about long lines, but it's best to keep lines fully visible inside the code tabs. You can easily break long function parameter lists with a newline after a comma, and you can also have a trailing comma after the last parameter if you want to add a newline there.

Clone this wiki locally