Skip to content

Commit

Permalink
Added channel examples
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed Jun 22, 2013
1 parent 7c60b87 commit ff60c4c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,50 @@ func ExampleEncodedConn_Subscribe() {
me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
c.Publish("hello", me)
}

// BindSendChan() allows binding of a Go channel to a nats
// subject for publish operations. The Encoder attached to the
// EncodedConn will be used for marshalling.
func ExampleEncodedConn_BindSendChan() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()

type person struct {
Name string
Address string
Age int
}

ch := make(chan *person)
c.BindSendChan("hello", ch)

me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
ch <- me
}

// BindRecvChan() allows binding of a Go channel to a nats
// subject for subscribe operations. The Encoder attached to the
// EncodedConn will be used for un-marshalling.
func ExampleEncodedConn_BindRecvChan() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()

type person struct {
Name string
Address string
Age int
}

ch := make(chan *person)
c.BindRecvChan("hello", ch)

me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
c.Publish("hello", me)

// Receive the publish directly on a channel
who := <- ch

fmt.Printf("%v says hello!\n", who)
}

0 comments on commit ff60c4c

Please sign in to comment.