From 83aacd12ee9f96a556836455323da36f4cd95162 Mon Sep 17 00:00:00 2001 From: Arseny Mitin Date: Tue, 17 Sep 2019 10:05:21 +0300 Subject: [PATCH] refactor: avoid using fmt.Sprintf for performance --- api.go | 5 ++--- bot.go | 9 ++++----- sendable.go | 2 +- webhook.go | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/api.go b/api.go index c7f04d1..0e472fb 100644 --- a/api.go +++ b/api.go @@ -3,7 +3,6 @@ package telebot import ( "bytes" "encoding/json" - "fmt" "io" "io/ioutil" "mime/multipart" @@ -19,7 +18,7 @@ import ( // Raw lets you call any method of Bot API manually. func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) { - url := fmt.Sprintf("%s/bot%s/%s", b.URL, b.Token, method) + url := b.URL+ "/bot" + b.Token+"/"+ method var buf bytes.Buffer if err := json.NewEncoder(&buf).Encode(payload); err != nil { @@ -118,7 +117,7 @@ func (b *Bot) sendFiles( return nil, wrapSystem(err) } - url := fmt.Sprintf("%s/bot%s/%s", b.URL, b.Token, method) + url := b.URL+ "/bot" + b.Token+"/"+ method req, err := http.NewRequest("POST", url, body) if err != nil { return nil, wrapSystem(err) diff --git a/bot.go b/bot.go index 979df33..86fdc7a 100644 --- a/bot.go +++ b/bot.go @@ -545,7 +545,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag } else if f.FileURL != "" { mediaRepr = f.FileURL } else if f.OnDisk() || f.FileReader != nil { - mediaRepr = fmt.Sprintf("attach://%d", i) + mediaRepr = "attach://" + strconv.Itoa(i) files[strconv.Itoa(i)] = *f } else { return nil, errors.Errorf( @@ -1041,8 +1041,7 @@ func (b *Bot) GetFile(file *File) (io.ReadCloser, error) { return nil, err } - url := fmt.Sprintf("%s/file/bot%s/%s", - b.URL, b.Token, f.FilePath) + url := b.URL + "/file/bot" + b.Token + "/"+ f.FilePath resp, err := http.Get(url) if err != nil { @@ -1062,7 +1061,7 @@ func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Messa messageID, chatID := message.MessageSig() params := map[string]string{ - "chat_id": fmt.Sprintf("%d", chatID), + "chat_id": strconv.FormatInt(chatID, 10), "message_id": messageID, } @@ -1355,7 +1354,7 @@ func (b *Bot) FileURLByID(fileID string) (string, error) { if err != nil { return "", err } - return fmt.Sprintf("%s/file/bot%s/%s", b.URL, b.Token, f.FilePath), nil + return b.URL + "/file/bot" + b.Token + "/"+ f.FilePath, nil } // UploadStickerFile returns uploaded File on success. diff --git a/sendable.go b/sendable.go index d6821e4..fb1801c 100644 --- a/sendable.go +++ b/sendable.go @@ -214,7 +214,7 @@ func (x *Location) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error "chat_id": to.Recipient(), "latitude": fmt.Sprintf("%f", x.Lat), "longitude": fmt.Sprintf("%f", x.Lng), - "live_period": fmt.Sprintf("%d", x.LivePeriod), + "live_period": strconv.Itoa(x.LivePeriod), } embedSendOptions(params, opt) diff --git a/webhook.go b/webhook.go index 9370c22..60e96e2 100644 --- a/webhook.go +++ b/webhook.go @@ -73,13 +73,13 @@ func (h *Webhook) getFiles() map[string]File { func (h *Webhook) getParams() map[string]string { param := make(map[string]string) if h.TLS != nil { - param["url"] = fmt.Sprintf("https://%s", h.Listen) + param["url"] = "https://" + h.Listen } else { // this will not work with telegram, they want TLS // but i allow this because telegram will send an error // when you register this hook. in their docs they write // that port 80/http is allowed ... - param["url"] = fmt.Sprintf("http://%s", h.Listen) + param["url"] = "http://" + h.Listen } if h.Endpoint != nil { param["url"] = h.Endpoint.PublicURL