MCPcopy Create free account
hub / github.com/google/go-cloud / Example_openVariableFromURL

Function Example_openVariableFromURL

runtimevar/example_openvariable_test.go:26–52  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

24)
25
26func Example_openVariableFromURL() {
27 // Connect to a Variable using a URL.
28 // This example uses "constantvar", an in-memory implementation.
29 // We need to add a blank import line to register the constantvar driver's
30 // URLOpener, which implements runtimevar.VariableURLOpener:
31 // import _ "gocloud.dev/runtimevar/constantvar"
32 // constantvar registers for the "constant" scheme.
33 // All runtimevar.OpenVariable URLs also work with "runtimevar+" or "runtimevar+variable+" prefixes,
34 // e.g., "runtimevar+constant://..." or "runtimevar+variable+constant://...".
35 ctx := context.Background()
36 v, err := runtimevar.OpenVariable(ctx, "constant://?val=hello+world&decoder=string")
37 if err != nil {
38 log.Fatal(err)
39 }
40 defer v.Close()
41
42 // Now we can use the Variable as normal.
43 snapshot, err := v.Latest(ctx)
44 if err != nil {
45 log.Fatal(err)
46 }
47 // It's safe to cast the Value to string since we used the string decoder.
48 fmt.Printf("%s\n", snapshot.Value.(string))
49
50 // Output:
51 // hello world
52}

Callers

nothing calls this directly

Calls 3

OpenVariableFunction · 0.92
LatestMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected