| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/nats-io/nats.go" |
| 8 |
) |
| 9 |
|
| 10 |
func main() { |
| 11 |
nc, _ := nats.Connect(nats.DefaultURL) |
| 12 |
defer nc.Close() |
| 13 |
|
| 14 |
// NATS-DOC-START |
| 15 |
// Request with custom timeout |
| 16 |
msg, err := nc.Request("service", []byte("data"), 2*time.Second) |
| 17 |
if err != nil { |
| 18 |
if err == nats.ErrTimeout { |
| 19 |
fmt.Println("Request timed out") |
| 20 |
} else { |
| 21 |
fmt.Printf("Request failed: %v\n", err) |
| 22 |
} |
| 23 |
return |
| 24 |
} |
| 25 |
fmt.Printf("Response: %s\n", string(msg.Data)) |
| 26 |
// NATS-DOC-END |
| 27 |
} |