Created
May 24, 2015 16:59
-
-
Save taotetek/9906323e701b113d2630 to your computer and use it in GitHub Desktop.
ReadWriter support in goczmq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/gob" | |
"fmt" | |
"github.com/zeromq/goczmq" | |
) | |
// person is an example type we will encode, send, and decode | |
type person struct { | |
Firstname string | |
Lastname string | |
Age int | |
} | |
func main() { | |
// First, we make a push socket with a czmq "smart" constructor. | |
// The "@" tells the constructor to bind. | |
push, err := goczmq.NewPush("@tcp://*:5555") | |
if err != nil { | |
panic(err) | |
} | |
defer push.Destroy() | |
// Now, let's make an encoder that will encode to this socket. | |
encoder := gob.NewEncoder(push) | |
// Next, we make a pull socket. | |
// The ">" tells the socket to connect. | |
pull, err := goczmq.NewPull(">tcp://127.0.0.1:5555") | |
if err != nil { | |
panic(err) | |
} | |
defer pull.Destroy() | |
// Now, let's make a decoder that will decode from this socket. | |
decoder := gob.NewDecoder(pull) | |
// Send an encoded type. | |
err = encoder.Encode(person{ | |
Firstname: "Arthur", | |
Lastname: "Dent", | |
Age: 30}) | |
if err != nil { | |
panic(err) | |
} | |
// Receive the message and decode it. | |
var p person | |
err = decoder.Decode(&p) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("%v\n", p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bryanl in a "real" scenario, I'd use a poller on the socket and then only call decode if there was a message waiting.