API Reference

nats.go

Go client for NATS core messaging, request-reply and JetStream persistence.

Official Documentation ↗
CompatibilityThis documentation covers version nats.go v1.37 / NATS Server 2.10+ / Go 1.20+. Check the official docs for the latest API surface.

Installation

gogo get github.com/nats-io/nats.go

Methods

Overview

nats.go connects Go applications to NATS for lightweight messaging, request-reply and durable JetStream streams. It handles reconnection, buffering and asynchronous delivery.

Core Methods

Connect with options, publish and subscribe on subjects, and use NC.Request for request-reply. nc.JetStream opens a context for persistent publish and consume operations.

Usage Notes

Use core NATS for high-rate, loss-tolerant signals and JetStream for durable messages. Use queue groups to load-balance subscribers and handle the closed callback for permanent disconnects.

Common Pitfalls

Core subscribers miss messages published before they subscribed. Unbounded outbound buffering during outages can exhaust memory. Forgetting to drain on shutdown drops in-flight messages.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/nats-io/nats.go@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:03.416Z.

FAQ

Does core NATS persist messages?
No. Core NATS is at-most-once and delivers only to current subscribers. JetStream adds streams, persistence, replay and acknowledgements.
How does request-reply work?
nc.Request publishes and waits for a reply on an inbox subject, returning the response message or a timeout error. The responder replies on the provided reply subject.
How do I handle reconnection?
nats.Connect reconnects by default with buffering of outbound messages. Configure max reconnect attempts, reconnect wait and a closed handler so the application can react to permanent loss.