go.etcd.io/etcd@v3.3.27+incompatible/client/example_keys_test.go (about)

     1  // Copyright 2017 The etcd 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  //     http://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 client_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"sort"
    22  
    23  	"github.com/coreos/etcd/client"
    24  )
    25  
    26  func ExampleKeysAPI_directory() {
    27  	c, err := client.New(client.Config{
    28  		Endpoints: exampleEndpoints,
    29  		Transport: exampleTransport,
    30  	})
    31  	if err != nil {
    32  		log.Fatal(err)
    33  	}
    34  	kapi := client.NewKeysAPI(c)
    35  
    36  	// Setting '/myNodes' to create a directory that will hold some keys.
    37  	o := client.SetOptions{Dir: true}
    38  	resp, err := kapi.Set(context.Background(), "/myNodes", "", &o)
    39  	if err != nil {
    40  		log.Fatal(err)
    41  	}
    42  
    43  	// Add keys to /myNodes directory.
    44  	resp, err = kapi.Set(context.Background(), "/myNodes/key1", "value1", nil)
    45  	if err != nil {
    46  		log.Fatal(err)
    47  	}
    48  	resp, err = kapi.Set(context.Background(), "/myNodes/key2", "value2", nil)
    49  	if err != nil {
    50  		log.Fatal(err)
    51  	}
    52  
    53  	// fetch directory
    54  	resp, err = kapi.Get(context.Background(), "/myNodes", nil)
    55  	if err != nil {
    56  		log.Fatal(err)
    57  	}
    58  	// print directory keys
    59  	sort.Sort(resp.Node.Nodes)
    60  	for _, n := range resp.Node.Nodes {
    61  		fmt.Printf("Key: %q, Value: %q\n", n.Key, n.Value)
    62  	}
    63  
    64  	// Output:
    65  	// Key: "/myNodes/key1", Value: "value1"
    66  	// Key: "/myNodes/key2", Value: "value2"
    67  }
    68  
    69  func ExampleKeysAPI_setget() {
    70  	c, err := client.New(client.Config{
    71  		Endpoints: exampleEndpoints,
    72  		Transport: exampleTransport,
    73  	})
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  	kapi := client.NewKeysAPI(c)
    78  
    79  	// Set key "/foo" to value "bar".
    80  	resp, err := kapi.Set(context.Background(), "/foo", "bar", nil)
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	// Get key "/foo"
    85  	resp, err = kapi.Get(context.Background(), "/foo", nil)
    86  	if err != nil {
    87  		log.Fatal(err)
    88  	}
    89  
    90  	fmt.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value)
    91  
    92  	// Output: "/foo" key has "bar" value
    93  }