golib

module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2021 License: Apache-2.0

README

Go Reference

golib

GoLib contains many useful golang tool kits. So it will be updated frequently.

migrationlib

Packaging this source: migrate

jwtlib

Packaging this source: jwt-go

mongolib

Packaging this source: mongo-go-driver

amqplib

Packaging this source: cony

pglib

Packaging this source: go-pg

redislib

Packaging this source: go-redis

uuidlib

Packaging this source: go.uuid

loglib

Packaging this source: zap

ratelimitlib

  • add redis implements slide window strategy

Contribution

  • provide your idea about this golib in issue or raise your PR request

todo

  • Add more lib
  • Add unit test in each lib
  • Write some comment

Directories

Path Synopsis
Package amqplib is for encapsulating github.com/assembla/cony any operations As a quick start for publisher: connectionConfig := &amqplib.AMQPConnectionConfig{ URL: "amqp://guest:guest@localhost/", ErrorHandler: nil, } queueConfig := &amqplib.AMQPQueueConfig{ ExchangeName: "test-exchange", ExchangeType: amqplib.Fanout, RoutingKey: "key", AutoDeclareExchange: true, AutoDelete: false, } client1 := amqplib.NewAMQPClient(connectionConfig) defer client1.Close() publisher, err := client1.NewPublisher(queueConfig) if err != nil { panic(err) } defer publisher.Close() err = publisher.Publish(amqp.Publishing{ Body: []byte("hello"), }) if err != nil { panic(err) } As a quick start for consumer: connectionConfig := &amqplib.AMQPConnectionConfig{ URL: "amqp://guest:guest@localhost/", ErrorHandler: nil, } queueConfig := &amqplib.AMQPQueueConfig{ ExchangeName: "test-exchange", ExchangeType: amqplib.Fanout, QueueName: "test-queue", AutoDelete: false, } client := amqplib.NewAMQPClient(connectionConfig) defer client1.Close() consumer, err := client.NewConsumer(queueConfig) if err != nil { panic(err) } defer consumer.Close() for delivery := range consumer.Consume() { fmt.Println(string(delivery.Body)) delivery.Ack(false) }
Package amqplib is for encapsulating github.com/assembla/cony any operations As a quick start for publisher: connectionConfig := &amqplib.AMQPConnectionConfig{ URL: "amqp://guest:guest@localhost/", ErrorHandler: nil, } queueConfig := &amqplib.AMQPQueueConfig{ ExchangeName: "test-exchange", ExchangeType: amqplib.Fanout, RoutingKey: "key", AutoDeclareExchange: true, AutoDelete: false, } client1 := amqplib.NewAMQPClient(connectionConfig) defer client1.Close() publisher, err := client1.NewPublisher(queueConfig) if err != nil { panic(err) } defer publisher.Close() err = publisher.Publish(amqp.Publishing{ Body: []byte("hello"), }) if err != nil { panic(err) } As a quick start for consumer: connectionConfig := &amqplib.AMQPConnectionConfig{ URL: "amqp://guest:guest@localhost/", ErrorHandler: nil, } queueConfig := &amqplib.AMQPQueueConfig{ ExchangeName: "test-exchange", ExchangeType: amqplib.Fanout, QueueName: "test-queue", AutoDelete: false, } client := amqplib.NewAMQPClient(connectionConfig) defer client1.Close() consumer, err := client.NewConsumer(queueConfig) if err != nil { panic(err) } defer consumer.Close() for delivery := range consumer.Consume() { fmt.Println(string(delivery.Body)) delivery.Ack(false) }
Package jwtlib is for encapsulating github.com/dgrijalva/jwt-go any operations As a quick start: userID := "user1" customPayload := map[string]interface{}{ "userID": userID, } jwtLib := jwtlib.NewJWTLib(jwtlib.Config{ Payload: customPayload, SignALG: jwtlib.HS256, SecretKey: []byte("secret"), TokenTimeout: time.Hour, }) token, err := jwtLib.Sign() if err != nil { panic(err) } fmt.Println(token) payload, err := jwtLib.Verify(token) if err != nil { panic(err) } fmt.Println(payload)
Package jwtlib is for encapsulating github.com/dgrijalva/jwt-go any operations As a quick start: userID := "user1" customPayload := map[string]interface{}{ "userID": userID, } jwtLib := jwtlib.NewJWTLib(jwtlib.Config{ Payload: customPayload, SignALG: jwtlib.HS256, SecretKey: []byte("secret"), TokenTimeout: time.Hour, }) token, err := jwtLib.Sign() if err != nil { panic(err) } fmt.Println(token) payload, err := jwtLib.Verify(token) if err != nil { panic(err) } fmt.Println(payload)
Package loglib is for encapsulating https://github.com/uber-go/zap any operations As a quick start for loglib: logger, err := loglib.NewProductionLogger() if err != nil { panic(err) } logger.Info("test logger", zap.String("hello", "world")) logger, err := loglib.NewProductionLogger() if err != nil { panic(err) } logger.Log(loglib.INFO, "test logger", "hello", "world")
Package loglib is for encapsulating https://github.com/uber-go/zap any operations As a quick start for loglib: logger, err := loglib.NewProductionLogger() if err != nil { panic(err) } logger.Info("test logger", zap.String("hello", "world")) logger, err := loglib.NewProductionLogger() if err != nil { panic(err) } logger.Log(loglib.INFO, "test logger", "hello", "world")
Package migrationlib is for encapsulating github.com/golang-migrate/migrate any operations As a quick start: migrationlib.NewMigrateLib(migrationlib.Config{ DatabaseDriver: migrationlib.PostgresDriver, DatabaseURL: "postgres://postgres:postgres@localhost:5432/migrationlib?sslmode=disable", SourceDriver: migrationlib.FileDriver, SourceURL: "file://migrations", TableName: "migration_versions", }).Up()
Package migrationlib is for encapsulating github.com/golang-migrate/migrate any operations As a quick start: migrationlib.NewMigrateLib(migrationlib.Config{ DatabaseDriver: migrationlib.PostgresDriver, DatabaseURL: "postgres://postgres:postgres@localhost:5432/migrationlib?sslmode=disable", SourceDriver: migrationlib.FileDriver, SourceURL: "file://migrations", TableName: "migration_versions", }).Up()
Package mongolib is for encapsulating go.mongodb.org/mongo-driver any operations As a quick start: ctx := context.Background() mgo, err := mongolib.NewMongoDriver(ctx, mongolib.Config{ AppName: "test", URL: "mongodb://localhost:27017", Database: "golang", }, nil) if err != nil { panic(err) } collection := mgo.Database.Collection("example") collection.find(...)
Package mongolib is for encapsulating go.mongodb.org/mongo-driver any operations As a quick start: ctx := context.Background() mgo, err := mongolib.NewMongoDriver(ctx, mongolib.Config{ AppName: "test", URL: "mongodb://localhost:27017", Database: "golang", }, nil) if err != nil { panic(err) } collection := mgo.Database.Collection("example") collection.find(...)
Package pglib is for encapsulating github.com/go-pg/pg any operations As a quick start: cfg := pglib.GOPGConfig{ URL: "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable", DebugMode: true, PoolSize: 5, } client, err := pglib.NewDefaultGOPGClient(cfg) if err != nil { panic(err) } fmt.Println(client.Ping(context.Background()))
Package pglib is for encapsulating github.com/go-pg/pg any operations As a quick start: cfg := pglib.GOPGConfig{ URL: "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable", DebugMode: true, PoolSize: 5, } client, err := pglib.NewDefaultGOPGClient(cfg) if err != nil { panic(err) } fmt.Println(client.Ping(context.Background()))
Package ratelimilib is for rate limit any strategy operations As a quick start: client, err := redislib.NewGORedisClient(redislib.GORedisConfig{URL: "redis://localhost:6379"}, nil) if err != nil { panic(err) } limiter := ratelimitlib.NewSlideWindowRateLimiter(client, 5, 30 * time.Second) t := time.Now().UnixNano() count, err := limiter.Incr(context.Background(), "kenny", t) if err != nil { panic(err) } fmt.Println(count)
Package ratelimilib is for rate limit any strategy operations As a quick start: client, err := redislib.NewGORedisClient(redislib.GORedisConfig{URL: "redis://localhost:6379"}, nil) if err != nil { panic(err) } limiter := ratelimitlib.NewSlideWindowRateLimiter(client, 5, 30 * time.Second) t := time.Now().UnixNano() count, err := limiter.Incr(context.Background(), "kenny", t) if err != nil { panic(err) } fmt.Println(count)
Package redislib is for encapsulating github.com/go-redis/redis any operations As a quick start: client, err := redislib.NewGORedisClient(redislib.GORedisConfig{URL: "redis://localhost:6379"}, nil) if err != nil { panic(err) } fmt.Println(client.Ping(context.Background()).Err()) client, err := redislib.NewGORedisClusterClient(redislib.GORedisClusterConfig{Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"}}, nil) if err != nil { panic(err) } fmt.Println(client.Ping(context.Background()).Err())
Package redislib is for encapsulating github.com/go-redis/redis any operations As a quick start: client, err := redislib.NewGORedisClient(redislib.GORedisConfig{URL: "redis://localhost:6379"}, nil) if err != nil { panic(err) } fmt.Println(client.Ping(context.Background()).Err()) client, err := redislib.NewGORedisClusterClient(redislib.GORedisClusterConfig{Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"}}, nil) if err != nil { panic(err) } fmt.Println(client.Ping(context.Background()).Err())
Package uuidlib is for encapsulating github.com/satori/go.uuid any operations As a quick start: // this is easy for unit test mock uv4 := uuidlib.GOUUIDGenerator{}.NewV4() // just for generate uv4 = uuidlib.NewV4() fmt.Println(uv4)
Package uuidlib is for encapsulating github.com/satori/go.uuid any operations As a quick start: // this is easy for unit test mock uv4 := uuidlib.GOUUIDGenerator{}.NewV4() // just for generate uv4 = uuidlib.NewV4() fmt.Println(uv4)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL