In this post, I will talk about an exciting project maintained by the team that develops the Go language: the storage in the tests, while in the production environment, we can use the cloud provider.
Different environments. We can use a cheaper supplier in a test/homologation environment and one more robust and expensive in the production environment.
Evolution. Your application may start with a more straightforward solution, say .
For example, let's look at the following code:
package main
import (
"context"
"fmt"
"gocloud.dev/blob"
"gocloud.dev/blob/memblob"
)
func main() {
ctx := context.Background()
// Create an in-memory bucket.
bucket := memblob.OpenBucket(nil)
defer bucket.Close()
key := "my-key"
text := []byte("hello world")
// Now we can use bucket to read or write files to the bucket.
err := write(ctx, bucket, key, text)
if err != nil {
panic(err)
}
data, err := read(ctx, bucket, key)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
func write(ctx context.Context, bucket *blob.Bucket, key string, text []byte) error {
err := bucket.WriteAll(ctx, key, text, nil)
return err
}
func read(ctx context.Context, bucket *blob.Bucket, key string) ([]byte, error) {
return bucket.ReadAll(ctx, key)
}
In the code, we are writing and reading from a document stored in a memory bucket. Therefore, to change the decision and use S3, it is only necessary to change the snippet below in the main function:
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-1"),
})
sess, err = session.NewSessionWithOptions(session.Options{
Profile: "profile_name_configured_in_your_machine",
Config: aws.Config{
Region: aws.String("us-east-1"),
},
})
if err != nil {
panic(err)
}
bucket, err := s3blob.OpenBucket(ctx, sess, "post-go-cdk", nil)
if err != nil {
panic(err)
}
defer bucket.Close()
We are setting up the S3 connection and creating a bucket on this provider. The rest of the code doesn't change. We continue using the read and write functions.
Currently, the project has abstractions for:
, i.e., document databases, with support for Google Cloud Firestore, Amazon DynamoDB, Azure Cosmos DB, MongoDB, and in-memory storage.
. Perhaps the most complete, with support for Google Cloud Pub/Sub, Amazon Simple Notification Service (SNS), Amazon Simple Queue Service (SQS), Azure Service Bus, RabbitMQ, NATS, Kafka and memory storage.- And the list goes on.
And in addition to code, the official website has an area with some important on January 10, 2023.
SOCIAL SHARE CARD GENERATOR