github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/topic/topicwriter/topicwriter_test.go (about)

     1  package topicwriter_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/ydb-platform/ydb-go-sdk/v3"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicwriter"
    12  )
    13  
    14  func ExampleWriter_Write() {
    15  	ctx := context.Background()
    16  	db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"))
    17  	if err != nil {
    18  		log.Fatalf("failed ydb connection: %v", err)
    19  	}
    20  
    21  	writer, err := db.Topic().StartWriter("topicName")
    22  	if err != nil {
    23  		log.Fatalf("failed to create topic writer: %v", err)
    24  	}
    25  
    26  	err = writer.Write(ctx,
    27  		topicwriter.Message{Data: strings.NewReader("1")},
    28  		topicwriter.Message{Data: strings.NewReader("2")},
    29  		topicwriter.Message{Data: strings.NewReader("3")},
    30  	)
    31  	if err == nil {
    32  		fmt.Println("OK")
    33  	} else {
    34  		log.Fatalf("failed write to stream")
    35  	}
    36  }