Skip to content

Commit 4cd212b

Browse files
authored
Hello world for Golang stream client (#383)
1 parent 0f18bb7 commit 4cd212b

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

go-stream/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Go code for RabbitMQ tutorials
2+
3+
4+
Here you can find Go code examples from [RabbitMQ tutorials](https://www.rabbitmq.com/getstarted.html).
5+
6+
7+
## Requirements
8+
9+
These examples use the [`rabbitmq/rabbitmq-stream-go-client`](https://github.com/rabbitmq/rabbitmq-stream-go-client) client library.
10+
Get it first with
11+
12+
go get -u github.com/rabbitmq/rabbitmq-stream-go-client
13+
14+
## Code
15+
16+
Code examples are executed via `go run`:
17+
18+
[Tutorial one: "Hello World!"](https://www.rabbitmq.com/tutorials/tutorial-one-go-stream.html):
19+
20+
go run send.go
21+
go run receive.go
22+
23+
24+
To learn more, see [`rabbitmq/rabbitmq-stream-go-client`](https://github.com/rabbitmq/rabbitmq-stream-go-client).

go-stream/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/rabbitmq/rabbitmq-tutorials
2+
3+
go 1.22.0
4+
5+
require github.com/rabbitmq/rabbitmq-stream-go-client v1.4.1
6+
7+
require (
8+
github.com/golang/snappy v0.0.4 // indirect
9+
github.com/klauspost/compress v1.17.8 // indirect
10+
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
11+
github.com/pkg/errors v0.9.1 // indirect
12+
github.com/spaolacci/murmur3 v1.1.0 // indirect
13+
)

go-stream/go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
2+
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
3+
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
4+
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
5+
github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9FV9ix19jjM=
6+
github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
7+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
8+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9+
github.com/rabbitmq/rabbitmq-stream-go-client v1.4.1 h1:QWHiXPio2rhQU98TKFhyooGWH2Xviwwz9RFGdIpIlUg=
10+
github.com/rabbitmq/rabbitmq-stream-go-client v1.4.1/go.mod h1:CbFPhxC2+ctPq0FmWD3CiZWWGiP7sP4joRpw4YTlyL4=
11+
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
12+
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

go-stream/receive.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
7+
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
8+
"os"
9+
)
10+
11+
func CheckErrReceive(err error) {
12+
if err != nil {
13+
fmt.Printf("%s ", err)
14+
os.Exit(1)
15+
}
16+
}
17+
func main() {
18+
19+
env, err := stream.NewEnvironment(
20+
stream.NewEnvironmentOptions().
21+
SetHost("localhost").
22+
SetPort(5552).
23+
SetUser("guest").
24+
SetPassword("guest"))
25+
CheckErrReceive(err)
26+
27+
streamName := "hello-go-stream"
28+
err = env.DeclareStream(streamName,
29+
&stream.StreamOptions{
30+
MaxLengthBytes: stream.ByteCapacity{}.GB(2),
31+
},
32+
)
33+
CheckErrReceive(err)
34+
35+
messagesHandler := func(consumerContext stream.ConsumerContext, message *amqp.Message) {
36+
fmt.Printf("Stream: %s - Received message: %s\n", consumerContext.Consumer.GetStreamName(),
37+
message.Data)
38+
}
39+
40+
consumer, err := env.NewConsumer(streamName, messagesHandler,
41+
stream.NewConsumerOptions().SetOffset(stream.OffsetSpecification{}.First()))
42+
CheckErrReceive(err)
43+
44+
reader := bufio.NewReader(os.Stdin)
45+
fmt.Println(" [x] Waiting for messages. enter to close the consumer")
46+
_, _ = reader.ReadString('\n')
47+
err = consumer.Close()
48+
CheckErrReceive(err)
49+
50+
}

go-stream/send.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
7+
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
8+
"os"
9+
)
10+
11+
func CheckErrSend(err error) {
12+
if err != nil {
13+
fmt.Printf("%s ", err)
14+
os.Exit(1)
15+
}
16+
}
17+
func main() {
18+
env, err := stream.NewEnvironment(
19+
stream.NewEnvironmentOptions().
20+
SetHost("localhost").
21+
SetPort(5552).
22+
SetUser("guest").
23+
SetPassword("guest"))
24+
CheckErrSend(err)
25+
26+
streamName := "hello-go-stream"
27+
err = env.DeclareStream(streamName,
28+
&stream.StreamOptions{
29+
MaxLengthBytes: stream.ByteCapacity{}.GB(2),
30+
},
31+
)
32+
CheckErrSend(err)
33+
34+
producer, err := env.NewProducer(streamName, stream.NewProducerOptions())
35+
CheckErrSend(err)
36+
37+
// Send a message
38+
err = producer.Send(amqp.NewMessage([]byte("Hello world")))
39+
CheckErrSend(err)
40+
fmt.Printf(" [x] 'Hello world' Message sent\n")
41+
42+
reader := bufio.NewReader(os.Stdin)
43+
fmt.Println(" [x] Press enter to close the producer")
44+
_, _ = reader.ReadString('\n')
45+
err = producer.Close()
46+
CheckErrSend(err)
47+
}

0 commit comments

Comments
 (0)