github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/blob/memblob/example_test.go (about)

     1  // Copyright 2018 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package memblob_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  
    22  	"gocloud.dev/blob"
    23  	"gocloud.dev/blob/memblob"
    24  )
    25  
    26  func ExampleOpenBucket() {
    27  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    28  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    29  	ctx := context.Background()
    30  
    31  	// Create an in-memory bucket.
    32  	bucket := memblob.OpenBucket(nil)
    33  	defer bucket.Close()
    34  
    35  	// Now we can use bucket to read or write files to the bucket.
    36  	err := bucket.WriteAll(ctx, "my-key", []byte("hello world"), nil)
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  	data, err := bucket.ReadAll(ctx, "my-key")
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  	fmt.Println(string(data))
    45  
    46  	// Output:
    47  	// hello world
    48  }
    49  
    50  func Example_openBucketFromURL() {
    51  	// blob.OpenBucket creates a *blob.Bucket from a URL.
    52  	b, err := blob.OpenBucket(context.Background(), "mem://")
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  	defer b.Close()
    57  
    58  	// Now we can use b to read or write files to the container.
    59  	ctx := context.Background()
    60  	err = b.WriteAll(ctx, "my-key", []byte("hello world"), nil)
    61  	if err != nil {
    62  		log.Fatal(err)
    63  	}
    64  	data, err := b.ReadAll(ctx, "my-key")
    65  	if err != nil {
    66  		log.Fatal(err)
    67  	}
    68  	fmt.Println(string(data))
    69  
    70  	// Output:
    71  	// hello world
    72  }