golang-echo-boilerplate icon indicating copy to clipboard operation
golang-echo-boilerplate copied to clipboard

Refactoring NewPostResponse

Open sOM2H opened this issue 4 years ago • 1 comments

I don't think we need to use append() since we know the number of posts and append() just slows down the function.

Better to do so:

func NewPostResponse(posts []models.Post) *[]PostResponse {
	postResponse := make([]PostResponse, len(posts))

	for i, p := range posts {
		postResponse[i] = PostResponse{
			Title:    p.Title,
			Content:  p.Content,
			Username: p.User.Name,
			ID:       p.ID,
		}
	}

	return &postResponse
}

sOM2H avatar Mar 22 '21 16:03 sOM2H

Hello, it is a good idea. Please, create a PR to add this improvement.

pechenini avatar Apr 14 '21 07:04 pechenini

not actual

taras-seryogin avatar Dec 17 '23 21:12 taras-seryogin