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:
parent
022cfd5cb1
commit
15ee94fbbe
7 changed files with 15 additions and 8 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue