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

Function ExampleVariable_Watch

runtimevar/example_test.go:147–179  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

145}
146
147func ExampleVariable_Watch() {
148 // Construct a *Variable using a constructor from one of the
149 // runtimevar subpackages. This example uses constantvar.
150 // The variable value is of type string, so we use StringDecoder.
151 v := constantvar.NewBytes([]byte("hello world"), runtimevar.StringDecoder)
152 defer v.Close()
153
154 // Call Watch in a loop from a background goroutine to see all changes,
155 // including errors.
156 //
157 // You can use this for logging, or to trigger behaviors when the
158 // config changes.
159 //
160 // Note that Latest always returns the latest "good" config, so seeing
161 // an error from Watch doesn't mean that Latest will return one.
162 go func() {
163 for {
164 snapshot, err := v.Watch(context.Background())
165 if err == runtimevar.ErrClosed {
166 // v has been closed; exit.
167 return
168 }
169 if err == nil {
170 // Casting to a string here because we used StringDecoder.
171 log.Printf("New config: %v", snapshot.Value.(string))
172 } else {
173 log.Printf("Error loading config: %v", err)
174 // Even though there's been an error loading the config,
175 // v.Latest will continue to return the latest "good" value.
176 }
177 }
178 }()
179}
180
181func ExampleDecryptDecode() {
182 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.

Callers

nothing calls this directly

Calls 3

NewBytesFunction · 0.92
WatchMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected