import ( "github.com/cockroachdb/cockroach-go/v2/crdb" "github.com/cockroachdb/cockroach-go/v2/crdb/crgrpc" "github.com/cockroachdb/cockroach-go/v2/testserver" "github.com/cockroachdb/cockroach/pkg/roachpb" ) func main() { // Create a BatchRequest containing a single Put request. req := &roachpb.BatchRequest{} req.Add(&roachpb.PutRequest{ Key: roachpb.Key("hello"), Value: roachpb.MakeValueFromString("world"), }) // Execute the request against a test CockroachDB cluster. ts, err := testserver.NewTestServer() if err != nil { panic(err) } defer ts.Stop() client := crgrpc.NewClient(ts.Server.TestGRPCServer.TestServer.ClientConn) _, err = crdb.ExecuteTx(client, func(tx *client.Txn) error { _, err := tx.Send(req) return err }) if err != nil { panic(err) } }
req := &roachpb.BatchRequest{} req.Add(&roachpb.PutRequest{ Key: roachpb.Key("key1"), Value: roachpb.MakeValueFromString("value1"), }) req.Add(&roachpb.PutRequest{ Key: roachpb.Key("key2"), Value: roachpb.MakeValueFromString("value2"), }) req.Add(&roachpb.PutRequest{ Key: roachpb.Key("key3"), Value: roachpb.MakeValueFromString("value3"), })This example creates a BatchRequest containing three Put requests, each inserting a different key-value pair into the database. Package library: github.com.cockroachdb.cockroach.roachpb is a part of the CockroachDB Go client library.