1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00
This commit is contained in:
ofekshenawa
2024-06-20 02:30:37 +03:00
commit 0b95fd7fa5
188 changed files with 45644 additions and 0 deletions

22
doctests/README.md Normal file
View File

@ -0,0 +1,22 @@
# Command examples for redis.io
These examples appear on the [Redis documentation](https://redis.io) site as part of the tabbed examples interface.
## How to add examples
- Create a Go test file with a meaningful name in the current folder.
- Create a single method prefixed with `Example` and write your test in it.
- Determine the id for the example you're creating and add it as the first line of the file: `// EXAMPLE: set_and_get`.
- We're using the [Testable Examples](https://go.dev/blog/examples) feature of Go to test the desired output has been written to stdout.
### Special markup
See https://github.com/redis-stack/redis-stack-website#readme for more details.
## How to test the examples
- Start a Redis server locally on port 6379
- CD into the `doctests` directory
- Run `go test` to test all examples in the directory.
- Run `go test filename.go` to test a single file

View File

@ -0,0 +1,48 @@
// EXAMPLE: lpush_and_lrange
// HIDE_START
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_LPush_and_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// HIDE_END
// REMOVE_START
errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
if errFlush != nil {
panic(errFlush)
}
// REMOVE_END
listSize, err := rdb.LPush(ctx, "my_bikes", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(listSize)
value, err := rdb.LRange(ctx, "my_bikes", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(value)
// HIDE_START
// Output: 2
// [bike:2 bike:1]
}
// HIDE_END

48
doctests/set_get_test.go Normal file
View File

@ -0,0 +1,48 @@
// EXAMPLE: set_and_get
// HIDE_START
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_Set_and_get() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
// HIDE_END
// REMOVE_START
errFlush := rdb.FlushDB(ctx).Err() // Clear the database before each test
if errFlush != nil {
panic(errFlush)
}
// REMOVE_END
err := rdb.Set(ctx, "bike:1", "Process 134", 0).Err()
if err != nil {
panic(err)
}
fmt.Println("OK")
value, err := rdb.Get(ctx, "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Printf("The name of the bike is %s", value)
// HIDE_START
// Output: OK
// The name of the bike is Process 134
}
// HIDE_END