GoLang中的NATS客户不会订阅

I have a very generic connection scrip to connect a nats server and just blindly print the message to the command line.

package main
import (
    "github.com/nats-io/go-nats"
    "fmt"
)


func main(){

    servers := "nats://URL:30401, nats://URL:30402, nats://URL:30403"
    nc, _ := nats.Connect(servers, nats.Token("TOKEN_KEY"))

    // Subscribe to AAPL trades
    nc.Subscribe("T.AAPL", func(m *nats.Msg){
        fmt.Printf("[TRADE] Received: %s
", string(m.Data))
    })

}

it builds fine and runs with out error, but wont actually subscribe. is the fmt.Printf the proper way to have the message print to terminal? or is there a bigger issue at hand here?

Subscribe create an asynchronous listener for events on that channel. As your main function exits straight after the call to subscribe there program will edit before the asynchronous process has finished. There is also synchronised subscribe function:

https://godoc.org/github.com/nats-io/go-nats#Conn.SubscribeSync

Or you can add a wait into your main method so that it doesn't exit straight away.

Assuming you are connecting ok (best to capture error on connect and check it) you are exiting the program since it does not wait to exit main since Subscribe creates async subscribers in their own Go routines. Use runtime.Goexit() to have the program wait. Similar to this example.