import ( "context" "github.com/coreos/etcd/client" ) // Connect to etcd cfg := client.Config{Endpoints: []string{"http://localhost:2379"}} c, err := client.New(cfg) // Create keysAPI keysAPI := client.NewKeysAPI(c) // Set a value for a key response, err := keysAPI.Set(context.Background(), "/example/key", "value", nil) if err != nil { // handle error }
import ( "context" "github.com/coreos/etcd/client" ) // Connect to etcd cfg := client.Config{Endpoints: []string{"http://localhost:2379"}} c, err := client.New(cfg) // Create keysAPI keysAPI := client.NewKeysAPI(c) // Set a value for a key with TTL response, err := keysAPI.Set(context.Background(), "/example/key", "value", &client.SetOptions{TTL: time.Second * 60}) if err != nil { // handle error }In this example, we set the value of a key but also specify a TTL (Time To Live) of 60 seconds. This means that the key will expire and be automatically deleted from etcd after 60 seconds. We achieve this by passing a SetOptions struct to the Set method with the TTL field set to 60 seconds.