github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/topic/README.md (about) 1 # `topic` - pure Go native client for [YDB Topic](https://ydb.tech/en/docs/concepts/topic) 2 [](https://github.com/ydb-platform/ydb/blob/main/LICENSE) 3 [](https://github.com/ydb-platform/ydb-go-sdk/releases) 4 [](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/topic) 5  6  7 [](https://goreportcard.com/report/github.com/ydb-platform/ydb-go-sdk/v3) 8 [](https://app.codecov.io/gh/ydb-platform/ydb-go-sdk) 9  10 [](https://github.com/ydb-platform/ydb-go-examples/tree/master/topic) 11 [](https://t.me/YDBPlatform) 12 [](https://ydb.tech) 13 14 See [ydb-go-sdk](https://github.com/ydb-platform/ydb-go-sdk) for describe all driver features. 15 16 `YDB` is an open-source Distributed SQL Database that combines high availability and scalability with strict consistency and [ACID](https://en.wikipedia.org/wiki/ACID) transactions with [CDC](https://en.wikipedia.org/wiki/Change_data_capture) and Data stream features. 17 18 ## Installation 19 20 ```sh 21 go get -u github.com/ydb-platform/ydb-go-sdk/v3 22 ``` 23 24 ## Example Usage <a name="example"></a> 25 * connect to YDB 26 ```golang 27 db, err := ydb.Open(ctx, "grpc://localhost:2136/local") 28 if err != nil { 29 log.Fatal(err) 30 } 31 ``` 32 33 * send messages 34 ```golang 35 producerAndGroupID := "group-id" 36 writer, err := db.Topic().StartWriter(producerAndGroupID, "topicName", 37 topicoptions.WithMessageGroupID(producerAndGroupID), 38 topicoptions.WithCodec(topictypes.CodecGzip), 39 ) 40 if err != nil { 41 log.Fatal(err) 42 } 43 44 data1 := []byte{1, 2, 3} 45 data2 := []byte{4, 5, 6} 46 mess1 := topicwriter.Message{Data: bytes.NewReader(data1)} 47 mess2 := topicwriter.Message{Data: bytes.NewReader(data2)} 48 49 err = writer.Write(ctx, mess1, mess2) 50 if err != nil { 51 log.Fatal(err) 52 } 53 ``` 54 55 * read messages 56 ```golang 57 for { 58 msg, err := reader.ReadMessage(ctx) 59 if err != nil { 60 log.Fatal(err) 61 } 62 content, err := io.ReadAll(msg) 63 if err != nil { 64 log.Fatal(err) 65 } 66 fmt.Println(string(content)) 67 err = reader.Commit(msg.Context(), msg) 68 if err != nil { 69 log.Fatal(err) 70 } 71 } 72 73 ```