From 46ce29dc34b300f1b704cfcb921d109bdff7ffc4 Mon Sep 17 00:00:00 2001 From: Alexander Babenko Date: Mon, 4 Jun 2018 18:10:13 +0300 Subject: [PATCH] Fix runtime error on v.client.Do() panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x96f422] When we handle webhooks from viber, we create instance by this way: v := &viber.Viber{ AppKey: token, Sender: viber.Sender{ Name: "MyPage", Avatar: "https://mysite.com/img/avatar.jpg", }, //Message: msgReceivedFunc, //Delivered: deliveredFunc, } May be this is bad way, and we need to use .New() method everywhere, beause v.client is nil. My fix: just check and init &http.Client{} if client is nil. --- request.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/request.go b/request.go index dbdf888..5a7579a 100644 --- a/request.go +++ b/request.go @@ -19,6 +19,10 @@ func (v *Viber) PostData(url string, i interface{}) ([]byte, error) { req, err := http.NewRequest("POST", url, bytes.NewBuffer(b)) req.Header.Add("X-Viber-Auth-Token", v.AppKey) req.Close = true + + if v.client == nil { + v.client = &http.Client{} + } resp, err := v.client.Do(req) if err != nil {