From 15ee94fbbeb7a13e21ea55b5aff5df235ee08bc3 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Tue, 29 Sep 2020 13:52:12 -0400 Subject: [PATCH] Replace fmt.Sprint with strconv.Itoa, specify slice capacity Uber's Go style guide outlines a slight performance benefit when using strconv over fmt: https://github.com/uber-go/guide/blob/dc025303c14891f54d1847396379548773e6123e/style.md#prefer-strconv-over-fmt Also specifiying slice capacity when it is known beforehand: https://github.com/uber-go/guide/blob/dc025303c14891f54d1847396379548773e6123e/style.md#specifying-slice-capacity Signed-off-by: Vartan Benohanian --- reddit/emoji.go | 6 ++++-- reddit/gold.go | 3 ++- reddit/multi.go | 5 +++-- reddit/post-and-comment.go | 4 ++-- reddit/post.go | 2 +- reddit/things.go | 1 + reddit/widget.go | 2 ++ 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/reddit/emoji.go b/reddit/emoji.go index dcbe507..7c72690 100644 --- a/reddit/emoji.go +++ b/reddit/emoji.go @@ -11,6 +11,7 @@ import ( "net/http" "net/url" "os" + "strconv" "strings" "github.com/google/go-querystring/query" @@ -63,6 +64,7 @@ func (e *emojis) UnmarshalJSON(data []byte) (err error) { return } + *e = make(emojis, 0, len(emojiMap)) for emojiName, emojiValue := range emojiMap { emoji := new(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) form := url.Values{} - form.Set("height", fmt.Sprint(height)) - form.Set("width", fmt.Sprint(width)) + form.Set("height", strconv.Itoa(height)) + form.Set("width", strconv.Itoa(width)) req, err := s.client.NewRequest(http.MethodPost, path, form) if err != nil { diff --git a/reddit/gold.go b/reddit/gold.go index e19e6d4..6b63b44 100644 --- a/reddit/gold.go +++ b/reddit/gold.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "strconv" ) // 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) form := url.Values{} - form.Set("months", fmt.Sprint(months)) + form.Set("months", strconv.Itoa(months)) req, err := s.client.NewRequest(http.MethodPost, path, form) if err != nil { diff --git a/reddit/multi.go b/reddit/multi.go index 6e1036d..6e7c3a8 100644 --- a/reddit/multi.go +++ b/reddit/multi.go @@ -58,8 +58,9 @@ func (n *SubredditNames) UnmarshalJSON(data []byte) error { return err } - for _, sr := range subreddits { - *n = append(*n, sr.Name) + *n = make(SubredditNames, len(subreddits)) + for i, sr := range subreddits { + (*n)[i] = sr.Name } return nil diff --git a/reddit/post-and-comment.go b/reddit/post-and-comment.go index 163f398..209604b 100644 --- a/reddit/post-and-comment.go +++ b/reddit/post-and-comment.go @@ -2,9 +2,9 @@ package reddit import ( "context" - "fmt" "net/http" "net/url" + "strconv" ) // 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.Set("id", id) - form.Set("dir", fmt.Sprint(vote)) + form.Set("dir", strconv.Itoa(int(vote))) form.Set("rank", "10") req, err := s.client.NewRequest(http.MethodPost, path, form) diff --git a/reddit/post.go b/reddit/post.go index 05ae19e..f76c8d7 100644 --- a/reddit/post.go +++ b/reddit/post.go @@ -339,7 +339,7 @@ func (s *PostService) PinToProfile(ctx context.Context, id string) (*Response, e form.Set("id", id) form.Set("state", "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) if err != nil { diff --git a/reddit/things.go b/reddit/things.go index c9ed724..dccf557 100644 --- a/reddit/things.go +++ b/reddit/things.go @@ -398,6 +398,7 @@ func (l *trophyList) UnmarshalJSON(b []byte) error { return err } + *l = make(trophyList, 0, len(root.Trophies)) for _, thing := range root.Trophies { if trophy, ok := thing.Trophy(); ok { *l = append(*l, trophy) diff --git a/reddit/widget.go b/reddit/widget.go index b4488a3..34436d6 100644 --- a/reddit/widget.go +++ b/reddit/widget.go @@ -91,6 +91,7 @@ func (l *WidgetList) UnmarshalJSON(data []byte) error { return err } + *l = make(WidgetList, 0, len(widgetMap)) for _, w := range widgetMap { root := new(rootWidget) err = json.Unmarshal(w, root) @@ -301,6 +302,7 @@ func (l *WidgetLinkList) UnmarshalJSON(data []byte) error { return err } + *l = make(WidgetLinkList, 0, len(dataMap)) for _, d := range dataMap { var widgetLinkDataMap map[string]json.RawMessage err = json.Unmarshal(d, &widgetLinkDataMap)