package main import ( "fmt" "github.com/hashicorp/consul/api" ) func main() { client, err := api.NewClient(api.DefaultConfig()) if err != nil { panic(err) } kv := client.KV() pair, _, err := kv.Get("my/key", nil) if err != nil { panic(err) } if pair == nil { fmt.Println("Key not found") } else { fmt.Printf("Value: %s\n", pair.Value) } }
package main import ( "fmt" "github.com/hashicorp/consul/api" ) func main() { client, err := api.NewClient(api.DefaultConfig()) if err != nil { panic(err) } kv := client.KV() pair := &api.KVPair{ Key: "my/key", Value: []byte("some value"), } _, err = kv.Put(pair, nil) if err != nil { panic(err) } fmt.Println("Value written successfully") }This example creates a Consul client, retrieves the Key-Value Store client, and writes a key-value pair to the KV store. The key is "my/key" and the value is "some value". If the write is successful, it will output "Value written successfully". Overall, these examples demonstrate how to use the Consul API client for the KV store. With it, you can easily read and write values to the KV store in your Consul cluster.