github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/runtimevar/httpvar/example_test.go (about)

     1  // Copyright 2019 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 httpvar_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  	"net/http"
    21  
    22  	"gocloud.dev/runtimevar"
    23  	"gocloud.dev/runtimevar/httpvar"
    24  )
    25  
    26  func ExampleOpenVariable() {
    27  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    28  
    29  	// Create an HTTP.Client
    30  	httpClient := http.DefaultClient
    31  
    32  	// Construct a *runtimevar.Variable that watches the page.
    33  	v, err := httpvar.OpenVariable(httpClient, "http://example.com", runtimevar.StringDecoder, nil)
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  	defer v.Close()
    38  }
    39  
    40  func Example_openVariableFromURL() {
    41  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    42  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/httpvar"
    43  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    44  	ctx := context.Background()
    45  
    46  	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
    47  	// The default opener connects to an etcd server based on the environment
    48  	// variable ETCD_SERVER_URL.
    49  
    50  	v, err := runtimevar.OpenVariable(ctx, "http://myserver.com/foo.txt?decoder=string")
    51  	if err != nil {
    52  		log.Fatal(err)
    53  	}
    54  	defer v.Close()
    55  }