gocloud.devgocloud.dev/blob/memblob Index | Examples | Files

package memblob

import "gocloud.dev/blob/memblob"

Package memblob provides an in-memory blob implementation. Use OpenBucket to construct a *blob.Bucket.

URLs

For blob.OpenBucket memblob registers for the scheme "mem". To customize the URL opener, or for more details on the URL format, see URLOpener. See https://gocloud.dev/concepts/urls/ for background information.

As

memblob does not support any types for As.

Example (OpenBucketFromURL)

Code:play 

package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/blob"
)

func main() {
	// blob.OpenBucket creates a *blob.Bucket from a URL.
	b, err := blob.OpenBucket(context.Background(), "mem://")
	if err != nil {
		log.Fatal(err)
	}
	defer b.Close()

	// Now we can use b to read or write files to the container.
	ctx := context.Background()
	err = b.WriteAll(ctx, "my-key", []byte("hello world"), nil)
	if err != nil {
		log.Fatal(err)
	}
	data, err := b.ReadAll(ctx, "my-key")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(data))

}

Output:

hello world

Index

Examples

Constants

const Scheme = "mem"

Scheme is the URL scheme memblob registers its URLOpener under on blob.DefaultMux.

Functions

func OpenBucket

func OpenBucket(opts *Options) *blob.Bucket

OpenBucket creates a *blob.Bucket backed by memory.

Example

Code:play 

package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/blob/memblob"
)

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// Create an in-memory bucket.
	bucket := memblob.OpenBucket(nil)
	defer bucket.Close()

	// Now we can use bucket to read or write files to the bucket.
	err := bucket.WriteAll(ctx, "my-key", []byte("hello world"), nil)
	if err != nil {
		log.Fatal(err)
	}
	data, err := bucket.ReadAll(ctx, "my-key")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(data))

}

Output:

hello world

Types

type Options

type Options struct{}

Options sets options for constructing a *blob.Bucket backed by memory.

type URLOpener

type URLOpener struct{}

URLOpener opens URLs like "mem://".

No query parameters are supported.

func (*URLOpener) OpenBucketURL

func (*URLOpener) OpenBucketURL(ctx context.Context, u *url.URL) (*blob.Bucket, error)

OpenBucketURL opens a blob.Bucket based on u.

Source Files

memblob.go

Version
v0.41.0 (latest)
Published
Mar 30, 2025
Platform
js/wasm
Imports
15 packages
Last checked
27 seconds ago

Tools for package owners.