Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created June 25, 2025 06:20
Show Gist options
  • Select an option

  • Save kuc-arc-f/bf224ff0e315058cfccfe0312ae6dcee to your computer and use it in GitHub Desktop.

Select an option

Save kuc-arc-f/bf224ff0e315058cfccfe0312ae6dcee to your computer and use it in GitHub Desktop.
golang + redis , example-3
module example.com/redis4
go 1.24.4
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/redis/go-redis/v9 v9.11.0 // indirect
)
package main
import (
"context"
"fmt"
"log"
"strconv"
"time"
"github.com/redis/go-redis/v9"
)
func main() {
start := time.Now()
ctx := context.Background()
// Redis クライアントの作成
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // パスワードなし
DB: 0, // DB0 使用
})
// 接続確認
pong, err := client.Ping(ctx).Result()
if err != nil {
log.Fatalf("Redis 接続失敗: %v", err)
}
fmt.Println("接続成功:", pong) // => PONG
// データの書き込み(SET)
var pos = 1
for i := 0; i < 1000; i++ {
pos = i + 1
targetKey := "k:" + strconv.Itoa(pos)
//fmt.Println(targetKey)
err = client.Set(ctx, targetKey, "Hello, Redis!", 0).Err()
if err != nil {
log.Fatalf("SET エラー: %v", err)
}
}
elapsed := time.Since(start)
fmt.Printf("処理時間: %s\n", elapsed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment