MCPcopy Index your code
hub / github.com/DoNewsCode/core / Example_reader

Function Example_reader

otkafka/example_test.go:15–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13)
14
15func Example_reader() {
16 if os.Getenv("KAFKA_ADDR") == "" {
17 fmt.Println("set KAFKA_ADDR to run this example")
18 return
19 }
20 brokers := strings.Split(os.Getenv("KAFKA_ADDR"), ",")
21 conf := map[string]interface{}{
22 "log": map[string]interface{}{
23 "level": "none",
24 },
25 "kafka": map[string]interface{}{
26 "reader": map[string]interface{}{
27 "default": otkafka.ReaderConfig{
28 Brokers: brokers,
29 Topic: "example",
30 },
31 },
32 "writer": map[string]interface{}{
33 "default": otkafka.WriterConfig{
34 Brokers: brokers,
35 Topic: "example",
36 },
37 },
38 },
39 }
40 c := core.Default(core.WithConfigStack(confmap.Provider(conf, "."), nil))
41 c.Provide(otkafka.Providers())
42 c.Invoke(func(writer *kafka.Writer) {
43 err := writer.WriteMessages(context.Background(), kafka.Message{Value: []byte(`hello`)})
44 if err != nil {
45 panic(err)
46 }
47 })
48 c.Invoke(func(reader *kafka.Reader) {
49 msg, err := reader.ReadMessage(context.Background())
50 if err != nil {
51 panic(err)
52 }
53 fmt.Println(string(msg.Value))
54 })
55 // Output:
56 // hello
57}

Callers

nothing calls this directly

Calls 6

DefaultFunction · 0.92
WithConfigStackFunction · 0.92
ProvidersFunction · 0.92
ProvideMethod · 0.80
InvokeMethod · 0.80
WriteMessagesMethod · 0.80

Tested by

no test coverage detected