goczmq
Introduction
A golang interface to the CZMQ v4.2 API.
Install
Dependencies
For CZMQ master
go get github.com/zeromq/goczmq
A Note on Build Tags
The CZMQ library includes experimental classes that are not built by default, but can be built
by passing --enable-drafts
to configure. Support for these draft classes are being added
to goczmq. To build these features against a CZMQ that has been compiled with --enable-drafts
,
use go build -tags draft
.
For CMZQ = 4.0
go get gopkg.in/zeromq/goczmq.v4
For CZMQ Before 4.0
go get gopkg.in/zeromq/goczmq.v1
Usage
Direct CZMQ Sock API
Example
package main
import (
"log"
"github.com/zeromq/goczmq"
)
func main() {
router, err := goczmq.NewRouter("tcp://*:5555")
if err != nil {
log.Fatal(err)
}
defer router.Destroy()
log.Println("router created and bound")
dealer, err := goczmq.NewDealer("tcp://127.0.0.1:5555")
if err != nil {
log.Fatal(err)
}
defer dealer.Destroy()
log.Println("dealer created and connected")
err = dealer.SendFrame([]byte("Hello"), goczmq.FlagNone)
if err != nil {
log.Fatal(err)
}
log.Println("dealer sent 'Hello'")
request, err := router.RecvMessage()
if err != nil {
log.Fatal(err)
}
log.Printf("router received '%s' from '%v'", request[1], request[0])
err = router.SendFrame(request[0], goczmq.FlagMore)
if err != nil {
log.Fatal(err)
}
log.Printf("router sent 'World'")
err = router.SendFrame([]byte("World"), goczmq.FlagNone)
if err != nil {
log.Fatal(err)
}
reply, err := dealer.RecvMessage()
if err != nil {
log.Fatal(err)
}
log.Printf("dealer received '%s'", string(reply[0]))
}
Output
2015/05/26 21:52:52 router created and bound
2015/05/26 21:52:52 dealer created and connected
2015/05/26 21:52:52 dealer sent 'Hello'
2015/05/26 21:52:52 router received 'Hello' from '[0 103 84 189 175]'
2015/05/26 21:52:52 router sent 'World'
2015/05/26 21:52:52 dealer received 'World'
io.ReadWriter support
Example
package main
import (
"log"
"github.com/zeromq/goczmq"
)
func main() {
router, err := goczmq.NewRouter("tcp://*:5555")
if err != nil {
log.Fatal(err)
}
defer router.Destroy()
log.Println("router created and bound")
dealer, err := goczmq.NewDealer("tcp://127.0.0.1:5555")
if err != nil {
log.Fatal(err)
}
defer dealer.Destroy()
log.Println("dealer created and connected")
n, err := dealer.Write([]byte("Hello"))
if err != nil {
log.Fatal(err)
}
log.Printf("dealer sent %d byte message 'Hello'\n", n)
buf := make([]byte, 16386)
n, err = router.Read(buf)
if err != nil {
log.Fatal(err)
}
log.Printf("router received '%s'\n", buf[:n])
n, err = router.Write([]byte("World"))
if err != nil {
log.Fatal(err)
}
log.Printf("router sent %d byte message 'World'\n", n)
n, err = dealer.Read(buf)
if err != nil {
log.Fatal(err)
}
log.Printf("dealer received '%s'", string(buf[:n]))
}
Output
2015/05/26 21:54:10 router created and bound
2015/05/26 21:54:10 dealer created and connected
2015/05/26 21:54:10 dealer sent 5 byte message 'Hello'
2015/05/26 21:54:10 router received 'Hello'
2015/05/26 21:54:10 router sent 5 byte message 'World'
2015/05/26 21:54:10 dealer received 'World'
Thread safe channel interface
Example
package main
import (
"log"
"github.com/zeromq/goczmq"
)
func main() {
router := goczmq.NewRouterChanneler("tcp://*:5555")
defer router.Destroy()
log.Println("router created and bound")
dealer := goczmq.NewDealerChanneler("tcp://127.0.0.1:5555")
defer dealer.Destroy()
log.Println("dealer created and connected")
dealer.SendChan <- [][]byte{[]byte("Hello")}
log.Println("dealer sent 'Hello'")
request := <-router.RecvChan
log.Printf("router received '%s' from '%v'", request[1], request[0])
router.SendChan <- [][]byte{request[0], []byte("World")}
log.Printf("router sent 'World'")
reply := <-dealer.RecvChan
log.Printf("dealer received '%s'", string(reply[0]))
}
Output
2015/05/26 21:56:43 router created and bound
2015/05/26 21:56:43 dealer created and connected
2015/05/26 21:56:43 dealer sent 'Hello'
2015/05/26 21:56:43 received 'Hello' from '[0 12 109 153 35]'
2015/05/26 21:56:43 router sent 'World'
2015/05/26 21:56:43 dealer received 'World'
GoDoc
godoc
See Also
License
This project uses the MPL v2 license, see LICENSE