Help us shape the future of Go! Take the 2026 Go Developer Survey

constantvar

package
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 33

Documentation

Overview

Package constantvar provides a runtimevar implementation with Variables that never change. Use New, NewBytes, NewFromEnv, or NewError to construct a *runtimevar.Variable.

URLs

For runtimevar.OpenVariable, constantvar registers for the scheme "constant". For more details on the URL format, see URLOpener. See https://gocloud.dev/concepts/urls/ for background information.

As

constantvar does not support any types for As.

Example (OpenVariableFromURL)
package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/runtimevar"
)

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, add a blank import: _ "gocloud.dev/runtimevar/constantvar"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
	// The constant value is in the URL param "val".
	v, err := runtimevar.OpenVariable(ctx, "constant://?val=hello+world&decoder=string")
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = v.Close() }()

	// The constant value is read from an environment variable specified in "envvar".
	v2, err := runtimevar.OpenVariable(ctx, "constant://?envvar=MY_ENVIRONMENT_VARIABLE&decoder=string")
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = v2.Close() }()
	// PRAGMA: On gocloud.dev, hide the rest of the function.
	snapshot, err := v.Latest(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(snapshot.Value.(string))

	// Output
	// hello world
}

Index

Examples

Constants

View Source
const Scheme = "constant"

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

Variables

This section is empty.

Functions

func New

func New(value any) *runtimevar.Variable

New constructs a *runtimevar.Variable holding value.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/runtimevar/constantvar"
)

func main() {
	// Construct a *runtimevar.Variable that always returns "hello world".
	v := constantvar.New("hello world")
	defer func() { _ = v.Close() }()

	// We can now read the current value of the variable from v.
	snapshot, err := v.Latest(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(snapshot.Value.(string))

}
Output:
hello world

func NewBytes added in v0.9.0

func NewBytes(b []byte, decoder *runtimevar.Decoder) *runtimevar.Variable

NewBytes uses decoder to decode b. If the decode succeeds, it constructs a *runtimevar.Variable holding the decoded value. If the decode fails, it constructs a runtimevar.Variable that always fails with the error.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"gocloud.dev/runtimevar"
	"gocloud.dev/runtimevar/constantvar"
)

func main() {
	// Construct a *runtimevar.Variable with a []byte.
	v := constantvar.NewBytes([]byte(`hello world`), runtimevar.BytesDecoder)
	defer func() { _ = v.Close() }()

	// We can now read the current value of the variable from v.
	snapshot, err := v.Latest(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("byte slice of length %d\n", len(snapshot.Value.([]byte)))

}
Output:
byte slice of length 11

func NewError

func NewError(err error) *runtimevar.Variable

NewError constructs a *runtimevar.Variable that always fails. Runtimevar wraps errors returned by drivers, so the error returned by runtimevar will not equal err.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"log"

	"gocloud.dev/runtimevar/constantvar"
)

func main() {
	// Construct a runtimevar.Variable that always returns errFake.
	errFake := errors.New("my error")
	v := constantvar.NewError(errFake)
	defer func() { _ = v.Close() }()

	// We can now use Watch to read the current value of the variable
	// from v. Note that Latest would block here since it waits for
	// a "good" value, and v will never get one.
	_, err := v.Watch(context.Background())
	if err == nil {
		log.Fatal("Expected an error!")
	}
	fmt.Println(err)

}
Output:
runtimevar (code=Unknown): my error

func NewFromEnv added in v0.35.0

func NewFromEnv(envVarName string, decoder *runtimevar.Decoder) *runtimevar.Variable

NewFromEnv reads an environment variable and uses decoder to decode it. If the decode succeeds, it constructs a *runtimevar.Variable holding the decoded value. If the decode fails, it constructs a runtimevar.Variable that always fails with the error. Note that the value of the constantvar is frozen at initialization time; it does not get a new value if the underlying environment variable value changes.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"gocloud.dev/runtimevar"
	"gocloud.dev/runtimevar/constantvar"
)

func main() {
	// Construct a *runtimevar.Variable with an environment variable name.
	_ = os.Setenv("MY_ENVIRONMENT_VARIABLE", "hello world")
	v := constantvar.NewFromEnv("MY_ENVIRONMENT_VARIABLE", runtimevar.BytesDecoder)
	defer func() { _ = v.Close() }()

	// We can now read the value from v.
	snapshot, err := v.Latest(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("byte slice of length %d\n", len(snapshot.Value.([]byte)))

}
Output:
byte slice of length 11

Types

type URLOpener added in v0.12.0

type URLOpener struct {
	// Decoder specifies the decoder to use if one is not specified in the URL.
	// Defaults to runtimevar.BytesDecoder.
	Decoder *runtimevar.Decoder
}

URLOpener opens constantvar URLs like "constant://?val=foo&decoder=string".

The host and path are ignored.

The following URL parameters are supported:

  • val: The value to use for the constant Variable. The bytes from val are passed to NewBytes.
  • envvar: The name of an environment variable to read the value from.
  • err: The error to use for the constant Variable. A new error is created using errors.New and passed to NewError.
  • decoder: The decoder to use. Defaults to runtimevar.BytesDecoder. See runtimevar.DecoderByName for supported values.

If multiple of "val", "envvar", or "err" are provided, "err" wins, then "envvar", then "val".

func (*URLOpener) OpenVariableURL added in v0.12.0

func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error)

OpenVariableURL opens the variable at the URL's path. See the package doc for more details.

Jump to

Keyboard shortcuts

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