Package goscriptor simplifies working with Redis scripts in Go.
go get github.com/yshengliao/goscriptorLet's start with a trivial example:
package main
import (
"github.com/yshengliao/goscriptor"
)
var (
scriptDefinition = "scriptKey|0.0.0"
hello = "hello"
_HelloworldTemplate = `
return 'Hello, World!'
`
)
type MyScriptor struct {
Scriptor *goscriptor.Scriptor
}
// hello function
func (s *MyScriptor) hello() (string, error) {
res, err := s.Scriptor.ExecSha(hello, []string{})
if err != nil {
return "", err
}
return res.(string), nil
}
func main() {
opt := &goscriptor.Option{
Host: "127.0.0.1",
Port: 6379,
Password: "",
DB: 0,
PoolSize: 10,
}
scripts := map[string]string{
hello: _HelloworldTemplate,
}
scriptor, err := goscriptor.NewDB(opt, 1, scriptDefinition, &scripts)
if err != nil {
panic(err)
}
myscript := &MyScriptor{
Scriptor: scriptor,
}
res, err := myscript.hello()
if err != nil {
panic(err)
}
println(res)
}-
testify
go get github.com/stretchr/testify -
go-redis
go get github.com/go-redis/redis/v8 -
miniredis
go get github.com/alicebob/miniredis/v2 -
null.v3
go get gopkg.in/guregu/null.v3
Before opening a pull request:
- Run
go test ./...to ensure all tests pass. - Format Go files with
gofmt -w. - Provide a clear summary of your changes in the PR description.
- Add test cases using "testify".
- Add redis script test method.
- script_test unit test.
- Improve or remove unused code (ongoing cleanup).
- Check code formatting.