Skip to content

πŸ”– Connection ​

go
import (
	"github.com/minio/minio-go/v7"
	"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.ConnectRedis("localhost:6379", "password", 0)

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

2. Connect redis uri ​

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

go
func ConnectRedisUri() {
	redis := gouse.ConnectRedisUri("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.ConnectPostgres("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.ConnectMongo(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.