Skip to content

Commit

Permalink
Bufferをつかいまわすようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Dec 15, 2023
1 parent 220f264 commit 9d75221
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions webapp/go/app.go
Expand Up @@ -687,38 +687,38 @@ var templatePostByteArray = [...][]byte{
[]byte(`"><input type="submit" name="submit" value="submit"> </form> </div> </div> </div>`),
}

// 850はtemplatePostByteArrayの合計サイズ
// 1024は適当
var templatePostBuf = bytes.NewBuffer(make([]byte, 0, 850 + 1024))

func templatePost(w io.Writer, post Post) {
createdAt := []byte(post.CreatedAt.Format(ISO8601Format))
postID := []byte(strconv.Itoa(post.ID))
userAccountName := []byte(post.User.AccountName)

// 850はtemplatePostByteArrayの合計サイズ
// 1024は適当
buf := bytes.NewBuffer(make([]byte, 0, 850 + 1024))

buf.Write(templatePostByteArray[0])
buf.Write(postID)
buf.Write(templatePostByteArray[1])
buf.Write(createdAt)
buf.Write(templatePostByteArray[2])
buf.Write(userAccountName)
buf.Write(templatePostByteArray[3])
buf.Write(userAccountName)
buf.Write(templatePostByteArray[4])
buf.Write(postID)
buf.Write(templatePostByteArray[5])
buf.Write(createdAt)
buf.Write(templatePostByteArray[6])
buf.Write([]byte(imageURL(post)))
buf.Write(templatePostByteArray[7])
buf.Write(userAccountName)
buf.Write(templatePostByteArray[8])
buf.Write(userAccountName)
buf.Write(templatePostByteArray[9])
buf.Write([]byte(post.Body))
buf.Write(templatePostByteArray[10])
buf.Write([]byte(strconv.Itoa(post.CommentCount)))
buf.Write(templatePostByteArray[11])
templatePostBuf.Write(templatePostByteArray[0])
templatePostBuf.Write(postID)
templatePostBuf.Write(templatePostByteArray[1])
templatePostBuf.Write(createdAt)
templatePostBuf.Write(templatePostByteArray[2])
templatePostBuf.Write(userAccountName)
templatePostBuf.Write(templatePostByteArray[3])
templatePostBuf.Write(userAccountName)
templatePostBuf.Write(templatePostByteArray[4])
templatePostBuf.Write(postID)
templatePostBuf.Write(templatePostByteArray[5])
templatePostBuf.Write(createdAt)
templatePostBuf.Write(templatePostByteArray[6])
templatePostBuf.Write([]byte(imageURL(post)))
templatePostBuf.Write(templatePostByteArray[7])
templatePostBuf.Write(userAccountName)
templatePostBuf.Write(templatePostByteArray[8])
templatePostBuf.Write(userAccountName)
templatePostBuf.Write(templatePostByteArray[9])
templatePostBuf.Write([]byte(post.Body))
templatePostBuf.Write(templatePostByteArray[10])
templatePostBuf.Write([]byte(strconv.Itoa(post.CommentCount)))
templatePostBuf.Write(templatePostByteArray[11])

// w.Write([]byte(
// fmt.Sprintf(`
Expand Down Expand Up @@ -758,13 +758,13 @@ func templatePost(w io.Writer, post Post) {
for _, c := range post.Comments {
userAccountName := []byte(c.User.AccountName)

buf.Write(templatePostByteArray[12])
buf.Write(userAccountName)
buf.Write(templatePostByteArray[13])
buf.Write(userAccountName)
buf.Write(templatePostByteArray[14])
buf.Write([]byte(c.Comment))
buf.Write(templatePostByteArray[15])
templatePostBuf.Write(templatePostByteArray[12])
templatePostBuf.Write(userAccountName)
templatePostBuf.Write(templatePostByteArray[13])
templatePostBuf.Write(userAccountName)
templatePostBuf.Write(templatePostByteArray[14])
templatePostBuf.Write([]byte(c.Comment))
templatePostBuf.Write(templatePostByteArray[15])
// w.Write([]byte(fmt.Sprintf(`
// <div class="isu-comment">
// <a href="/@%s" class="isu-comment-account-name">%s</a>
Expand All @@ -777,11 +777,11 @@ func templatePost(w io.Writer, post Post) {
// )))
}

buf.Write(templatePostByteArray[16])
buf.Write(postID)
buf.Write(templatePostByteArray[17])
buf.Write([]byte(post.CSRFToken))
buf.Write(templatePostByteArray[18])
templatePostBuf.Write(templatePostByteArray[16])
templatePostBuf.Write(postID)
templatePostBuf.Write(templatePostByteArray[17])
templatePostBuf.Write([]byte(post.CSRFToken))
templatePostBuf.Write(templatePostByteArray[18])
// w.Write([]byte(fmt.Sprintf(
// `<div class="isu-comment-form"> <form method="post" action="/comment"> <input type="text" name="comment">
// <input type="hidden" name="post_id" value="%d">
Expand All @@ -793,6 +793,7 @@ func templatePost(w io.Writer, post Post) {
// )))

templatePostBuf.WriteTo(w)
templatePostBuf.Reset()
}

func getAccountName(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 9d75221

Please sign in to comment.