()
| 24 | ) |
| 25 | |
| 26 | func 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 | } |
nothing calls this directly
no test coverage detected