If there’s an error, the client object will be nil. You are trying to call a method on a nil pointer.
This is how you should do it:
client, err := as.NewClient("127.0.0.1", 3000)
if err != nil {
// handle error here
// probably log and exit the app, or maybe retry
}
// we now know there was no error; so we can use our pointer
defer client.Close()
I strongly recommend reading a bit more about Go from the golang.org before continuing your app. That’s a couple of days of time well spent, and learning the idiomatic way of writing Go programs will pay off in the long run.
check(err) // This is my custom function for checking error
func check(err error) {
if err != nil {
fmt.println("Error: "+err.Error())
panic("gone")
}
}
client, err := as.NewClient("127.0.0.1", 3000)
// this function will run when program is exited on press of ctrl-c/cmd-c
app.RegisterOnInterrupt(func() {
// check if client is connected
if client.IsConnected() {
client.Close()
}
})
defer client.Close()
Let me know, if I can use client.IsConnected() function to check and than close on true.?
how can I find the real culprit, if client is nil, if err would have been nil on first case, it would have immediately thrown an error, but this error comes sometimes when I close the app using ctrl-c in my ubuntu.