Go Integration

Connect to Arctickey from Go using the official go-redis client.

Installation#

Terminal
go get github.com/redis/go-redis/v9

Quick Start#

Go
package main import ( "context" "crypto/tls" "fmt" "github.com/redis/go-redis/v9" ) func main() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "YOUR_INSTANCE.eu.arctickey.com:6379", Password: "YOUR_PASSWORD", TLSConfig: &tls.Config{ MinVersion: tls.VersionTLS12, }, }) // Test connection pong, err := rdb.Ping(ctx).Result() if err != nil { panic(err) } fmt.Println(pong) // PONG // Set a value err = rdb.Set(ctx, "key", "value", 0).Err() if err != nil { panic(err) } // Get a value val, err := rdb.Get(ctx, "key").Result() if err != nil { panic(err) } fmt.Println("key:", val) }

Using URL Connection String#

Go
opt, err := redis.ParseURL("rediss://:YOUR_PASSWORD@YOUR_INSTANCE.eu.arctickey.com:6379") if err != nil { panic(err) } rdb := redis.NewClient(opt)

Environment Variables#

Go
import "os" func newRedisClient() *redis.Client { opt, _ := redis.ParseURL(os.Getenv("REDIS_URL")) return redis.NewClient(opt) }
Terminal
export REDIS_URL="rediss://:YOUR_PASSWORD@YOUR_INSTANCE.eu.arctickey.com:6379"

Common Operations#

Strings#

Go
// SET with expiration err := rdb.Set(ctx, "session:123", "user-data", 24*time.Hour).Err() // GET val, err := rdb.Get(ctx, "session:123").Result() if err == redis.Nil { fmt.Println("key does not exist") } else if err != nil { panic(err) } // INCR newVal, err := rdb.Incr(ctx, "counter").Result()

Hashes#

Go
// HSET err := rdb.HSet(ctx, "user:1", map[string]interface{}{ "name": "Alice", "email": "alice@example.com", "age": 30, }).Err() // HGET name, err := rdb.HGet(ctx, "user:1", "name").Result() // HGETALL user, err := rdb.HGetAll(ctx, "user:1").Result() // user is map[string]string

Lists#

Go
// LPUSH err := rdb.LPush(ctx, "queue:tasks", "task1", "task2").Err() // RPOP (blocking) task, err := rdb.BRPop(ctx, 0, "queue:tasks").Result() // LRANGE tasks, err := rdb.LRange(ctx, "queue:tasks", 0, -1).Result()

Sets#

Go
// SADD err := rdb.SAdd(ctx, "tags:article:1", "go", "redis", "tutorial").Err() // SMEMBERS tags, err := rdb.SMembers(ctx, "tags:article:1").Result() // SISMEMBER exists, err := rdb.SIsMember(ctx, "tags:article:1", "go").Result()

Connection Pooling#

go-redis handles connection pooling automatically:

Go
rdb := redis.NewClient(&redis.Options{ Addr: "YOUR_INSTANCE.eu.arctickey.com:6379", Password: "YOUR_PASSWORD", TLSConfig: &tls.Config{}, // Pool settings PoolSize: 10, // Max connections MinIdleConns: 5, // Min idle connections PoolTimeout: 4 * time.Second, // Wait time for connection })

Pub/Sub#

Go
// Subscriber pubsub := rdb.Subscribe(ctx, "notifications") defer pubsub.Close() ch := pubsub.Channel() for msg := range ch { fmt.Println(msg.Channel, msg.Payload) } // Publisher (separate goroutine/service) err := rdb.Publish(ctx, "notifications", "Hello!").Err()

Pipelines#

Batch multiple commands for better performance:

Go
pipe := rdb.Pipeline() incr := pipe.Incr(ctx, "counter") pipe.Expire(ctx, "counter", time.Hour) get := pipe.Get(ctx, "other-key") _, err := pipe.Exec(ctx) if err != nil { panic(err) } fmt.Println(incr.Val()) fmt.Println(get.Val())

Transactions#

Go
// Watch and transaction err := rdb.Watch(ctx, func(tx *redis.Tx) error { n, err := tx.Get(ctx, "counter").Int() if err != nil && err != redis.Nil { return err } _, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error { pipe.Set(ctx, "counter", n+1, 0) return nil }) return err }, "counter")

Error Handling#

Go
val, err := rdb.Get(ctx, "key").Result() switch { case err == redis.Nil: fmt.Println("key does not exist") case err != nil: fmt.Println("error:", err) default: fmt.Println("value:", val) }

Testing#

Go
// Use miniredis for testing import "github.com/alicebob/miniredis/v2" func TestMyFunc(t *testing.T) { s := miniredis.RunT(t) rdb := redis.NewClient(&redis.Options{ Addr: s.Addr(), }) // Your tests... }