github.com/thiagoyeds/go-cloud@v0.26.0/runtimevar/constantvar/example_test.go (about) 1 // Copyright 2018 The Go Cloud Development Kit Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package constantvar_test 16 17 import ( 18 "context" 19 "errors" 20 "fmt" 21 "log" 22 23 "gocloud.dev/runtimevar" 24 "gocloud.dev/runtimevar/constantvar" 25 ) 26 27 func ExampleNew() { 28 // Construct a *runtimevar.Variable that always returns "hello world". 29 v := constantvar.New("hello world") 30 defer v.Close() 31 32 // We can now read the current value of the variable from v. 33 snapshot, err := v.Latest(context.Background()) 34 if err != nil { 35 log.Fatal(err) 36 } 37 fmt.Println(snapshot.Value.(string)) 38 39 // Output: 40 // hello world 41 } 42 43 func ExampleNewBytes() { 44 // Construct a *runtimevar.Variable with a []byte. 45 v := constantvar.NewBytes([]byte(`hello world`), runtimevar.BytesDecoder) 46 defer v.Close() 47 48 // We can now read the current value of the variable from v. 49 snapshot, err := v.Latest(context.Background()) 50 if err != nil { 51 log.Fatal(err) 52 } 53 fmt.Printf("byte slice of length %d\n", len(snapshot.Value.([]byte))) 54 55 // Output: 56 // byte slice of length 11 57 } 58 59 func ExampleNewError() { 60 // Construct a runtimevar.Variable that always returns errFake. 61 var errFake = errors.New("my error") 62 v := constantvar.NewError(errFake) 63 defer v.Close() 64 65 // We can now use Watch to read the current value of the variable 66 // from v. Note that Latest would block here since it waits for 67 // a "good" value, and v will never get one. 68 _, err := v.Watch(context.Background()) 69 if err == nil { 70 log.Fatal("Expected an error!") 71 } 72 fmt.Println(err) 73 74 // Output: 75 // runtimevar (code=Unknown): my error 76 } 77 78 func Example_openVariableFromURL() { 79 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 80 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/constantvar" 81 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 82 ctx := context.Background() 83 84 // runtimevar.OpenVariable creates a *runtimevar.Variable from a URL. 85 86 v, err := runtimevar.OpenVariable(ctx, "constant://?val=hello+world&decoder=string") 87 if err != nil { 88 log.Fatal(err) 89 } 90 defer v.Close() 91 // PRAGMA: On gocloud.dev, hide the rest of the function. 92 snapshot, err := v.Latest(ctx) 93 if err != nil { 94 log.Fatal(err) 95 } 96 fmt.Println(snapshot.Value.(string)) 97 98 // Output 99 // hello world 100 }