Skip to content

πŸ”– Connection ​

go
import (
	"github.com/thuongtruong109/gouse"
)

1. Connect redis ​

Description: Connect to Redis(using parameters)
Input params: (address string, password string, dbNo int)

go
func ConnectRedis() {
	redis := gouse.Redis("localhost:6379", "password", 0)

	// ... do something with redis
	redis.Set(redis.Context(), "key", "value", 0)
}

2. Connect redis uri ​

Description: Connect to Redis cloud (using uri)
Input params: (uri string)

go
func ConnectRedisUri() {
	redis := gouse.RedisCloud("redis://localhost:6379/0")

	// ... do something with redis
	redis.Set(redis.Context(), "key", "value", 0)

}

3. Connect postgres ​

Description: Connect to Postgres
Input params: (uri string)

go
func ConnectPostgres() {
	pg, err := gouse.Postgres("localhost:5432")
	if err != nil {
		panic(err)
	}

	// ... do something with pg
	pg.Client.QueryRow("SELECT * FROM users")
}

4. Connect mongo ​

Description: Connect to MongoDB
Input params: (context.Context, uri string)

go
func ConnectMongo() {
	ctx := gouse.CtxBg

	mongoClient, err := gouse.Mongo(ctx, "mongodb://localhost:27017")
	if err != nil {
		panic(err)
	}

	// ... do something with mongo
	mongoClient.Database("test").Collection("users").FindOne(ctx, nil)
}

5. Connect minio ​

Description: Connect to Minio
Input params: (context.Context, config gouse.MinioConfig)

go
func ConnectMinio() {
	// ctx := gouse.CtxBg

	// minioClient, err := gouse.ConnectMinio(ctx, gouse.MinioConf{
	// 	Endpoint:  "localhost:9000",
	// 	AccessKey: "minio",
	// 	SecretKey: "minio123",
	// 	UseSSL:    false,
	// 	Bucket:    "bucket",
	// 	Location:  "us-east-1",
	// })
	// if err != nil {
	// 	panic(err)
	// }

	// // ... do something with minio
	// minioClient.FPutObject(ctx, "bucket", "object", "file", minio.PutObjectOptions{
	// 	ContentType: "application/octet-stream",
	// })
}

Released under the MIT License.