Replace fmt.Sprint with strconv.Itoa, specify slice capacity

Uber's Go style guide outlines a slight performance benefit when using
strconv over fmt:
dc025303c1/style.md (prefer-strconv-over-fmt)

Also specifiying slice capacity when it is known beforehand:
dc025303c1/style.md (specifying-slice-capacity)

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-09-29 13:52:12 -04:00
parent 022cfd5cb1
commit 15ee94fbbe
7 changed files with 15 additions and 8 deletions

View file

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strconv"
"strings" "strings"
"github.com/google/go-querystring/query" "github.com/google/go-querystring/query"
@ -63,6 +64,7 @@ func (e *emojis) UnmarshalJSON(data []byte) (err error) {
return return
} }
*e = make(emojis, 0, len(emojiMap))
for emojiName, emojiValue := range emojiMap { for emojiName, emojiValue := range emojiMap {
emoji := new(Emoji) emoji := new(Emoji)
err = json.Unmarshal(emojiValue, emoji) err = json.Unmarshal(emojiValue, emoji)
@ -120,8 +122,8 @@ func (s *EmojiService) SetSize(ctx context.Context, subreddit string, height, wi
path := fmt.Sprintf("api/v1/%s/emoji_custom_size", subreddit) path := fmt.Sprintf("api/v1/%s/emoji_custom_size", subreddit)
form := url.Values{} form := url.Values{}
form.Set("height", fmt.Sprint(height)) form.Set("height", strconv.Itoa(height))
form.Set("width", fmt.Sprint(width)) form.Set("width", strconv.Itoa(width))
req, err := s.client.NewRequest(http.MethodPost, path, form) req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil { if err != nil {

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
) )
// GoldService handles communication with the gold // GoldService handles communication with the gold
@ -37,7 +38,7 @@ func (s *GoldService) Give(ctx context.Context, username string, months int) (*R
path := fmt.Sprintf("api/v1/gold/give/%s", username) path := fmt.Sprintf("api/v1/gold/give/%s", username)
form := url.Values{} form := url.Values{}
form.Set("months", fmt.Sprint(months)) form.Set("months", strconv.Itoa(months))
req, err := s.client.NewRequest(http.MethodPost, path, form) req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil { if err != nil {

View file

@ -58,8 +58,9 @@ func (n *SubredditNames) UnmarshalJSON(data []byte) error {
return err return err
} }
for _, sr := range subreddits { *n = make(SubredditNames, len(subreddits))
*n = append(*n, sr.Name) for i, sr := range subreddits {
(*n)[i] = sr.Name
} }
return nil return nil

View file

@ -2,9 +2,9 @@ package reddit
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
) )
// postAndCommentService handles communication with the post and comment // postAndCommentService handles communication with the post and comment
@ -137,7 +137,7 @@ func (s *postAndCommentService) vote(ctx context.Context, id string, vote vote)
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
form.Set("dir", fmt.Sprint(vote)) form.Set("dir", strconv.Itoa(int(vote)))
form.Set("rank", "10") form.Set("rank", "10")
req, err := s.client.NewRequest(http.MethodPost, path, form) req, err := s.client.NewRequest(http.MethodPost, path, form)

View file

@ -339,7 +339,7 @@ func (s *PostService) PinToProfile(ctx context.Context, id string) (*Response, e
form.Set("id", id) form.Set("id", id)
form.Set("state", "true") form.Set("state", "true")
form.Set("to_profile", "true") form.Set("to_profile", "true")
// form.Set("num", fmt.Sprint(pos)) // form.Set("num", strconv.Itoa(pos))
req, err := s.client.NewRequest(http.MethodPost, path, form) req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil { if err != nil {

View file

@ -398,6 +398,7 @@ func (l *trophyList) UnmarshalJSON(b []byte) error {
return err return err
} }
*l = make(trophyList, 0, len(root.Trophies))
for _, thing := range root.Trophies { for _, thing := range root.Trophies {
if trophy, ok := thing.Trophy(); ok { if trophy, ok := thing.Trophy(); ok {
*l = append(*l, trophy) *l = append(*l, trophy)

View file

@ -91,6 +91,7 @@ func (l *WidgetList) UnmarshalJSON(data []byte) error {
return err return err
} }
*l = make(WidgetList, 0, len(widgetMap))
for _, w := range widgetMap { for _, w := range widgetMap {
root := new(rootWidget) root := new(rootWidget)
err = json.Unmarshal(w, root) err = json.Unmarshal(w, root)
@ -301,6 +302,7 @@ func (l *WidgetLinkList) UnmarshalJSON(data []byte) error {
return err return err
} }
*l = make(WidgetLinkList, 0, len(dataMap))
for _, d := range dataMap { for _, d := range dataMap {
var widgetLinkDataMap map[string]json.RawMessage var widgetLinkDataMap map[string]json.RawMessage
err = json.Unmarshal(d, &widgetLinkDataMap) err = json.Unmarshal(d, &widgetLinkDataMap)