本文整理匯總了Golang中util.NewBuffer函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewBuffer函數的具體用法?Golang NewBuffer怎麽用?Golang NewBuffer使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewBuffer函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PostComment
// 發表評論。入topics_reply庫,更新topics和topics_ex庫
// objname 注冊的評論對象名
func PostComment(objid, objtype, uid int, content string, objname string) error {
comment := model.NewComment()
comment.Objid = objid
comment.Objtype = objtype
comment.Uid = uid
comment.Content = content
// TODO:評論樓層怎麽處理,避免衝突?最後的樓層信息保存在內存中?
// 暫時隻是從數據庫中取出最後的評論樓層
stringBuilder := util.NewBuffer()
stringBuilder.Append("objid=").AppendInt(objid).Append(" AND objtype=").AppendInt(objtype)
tmpCmt, err := model.NewComment().Where(stringBuilder.String()).Order("ctime DESC").Find()
if err != nil {
logger.Errorln("post comment service error:", err)
return err
} else {
comment.Floor = tmpCmt.Floor + 1
}
// 入評論庫
cid, err := comment.Insert()
if err != nil {
logger.Errorln("post comment service error:", err)
return err
}
// 回調,不關心處理結果(有些對象可能不需要回調)
if commenter, ok := commenters[objname]; ok {
logger.Debugf("評論[objid:%d] [objtype:%d] [uid:%d] 成功,通知被評論者更新", objid, objtype, uid)
go commenter.UpdateComment(cid, objid, uid, time.Now().Format("2006-01-02 15:04:05"))
}
return nil
}
示例2: UpdateComment
// 更新該帖子的回複信息
// cid:評論id;objid:被評論對象id;uid:評論者;cmttime:評論時間
func (self TopicComment) UpdateComment(cid, objid, uid int, cmttime string) {
tid := strconv.Itoa(objid)
// 更新最後回複信息
stringBuilder := util.NewBuffer().Append("lastreplyuid=").AppendInt(uid).Append(",lastreplytime=").Append(cmttime)
err := model.NewTopic().Set(stringBuilder.String()).Where("tid=" + tid).Update()
if err != nil {
logger.Errorln("更新帖子最後回複人信息失敗:", err)
}
// 更新回複數(TODO:暫時每次都更新表)
IncrTopicReply(tid, uid)
}
示例3: PostComment
// 發表評論(或回複)。
// objid 注冊的評論對象
// uid 評論人
func PostComment(uid, objid int, form url.Values) (*model.Comment, error) {
comment := model.NewComment()
comment.Objid = objid
objtype := util.MustInt(form.Get("objtype"))
comment.Objtype = objtype
comment.Uid = uid
comment.Content = form.Get("content")
// TODO:評論樓層怎麽處理,避免衝突?最後的樓層信息保存在內存中?
// 暫時隻是從數據庫中取出最後的評論樓層
stringBuilder := util.NewBuffer()
stringBuilder.Append("objid=").AppendInt(objid).Append(" AND objtype=").AppendInt(objtype)
tmpCmt, err := model.NewComment().Where(stringBuilder.String()).Order("ctime DESC").Find()
if err != nil {
logger.Errorln("post comment service error:", err)
return nil, err
} else {
comment.Floor = tmpCmt.Floor + 1
}
// 入評論庫
cid, err := comment.Insert()
if err != nil {
logger.Errorln("post comment service error:", err)
return nil, err
}
comment.Cid = cid
comment.Ctime = util.TimeNow()
decodeCmtContent(comment)
// 回調,不關心處理結果(有些對象可能不需要回調)
if commenter, ok := commenters[objtype]; ok {
logger.Debugf("評論[objid:%d] [objtype:%d] [uid:%d] 成功,通知被評論者更新", objid, objtype, uid)
go commenter.UpdateComment(cid, objid, uid, time.Now().Format("2006-01-02 15:04:05"))
}
// 發評論,活躍度+5
go IncUserWeight("uid="+strconv.Itoa(uid), 5)
// 給被評論對象所有者發係統消息
ext := map[string]interface{}{
"objid": objid,
"objtype": objtype,
"cid": cid,
"uid": uid,
}
go SendSystemMsgTo(0, objtype, ext)
// @某人 發係統消息
go SendSysMsgAtUids(form.Get("uid"), ext)
go SendSysMsgAtUsernames(form.Get("usernames"), ext)
return comment, nil
}
示例4: GenSetClause
// 構造update語句中的set部分子句
func GenSetClause(form url.Values, fields []string) string {
stringBuilder := util.NewBuffer()
for _, field := range fields {
if form.Get(field) != "" {
stringBuilder.Append(",").Append(field).Append("=").Append(form.Get(field))
}
}
if stringBuilder.Len() > 0 {
return stringBuilder.String()[1:]
}
return ""
}
示例5: Where
// 查詢條件處理(TODO:暫時沒有處理between和in)
func (this *Dao) Where(condition string) {
this.whereVal = make([]interface{}, 0)
stringBuilder := util.NewBuffer()
conditions := SplitIn(condition, []string{" and ", " AND ", " or ", " OR "}...)
for _, condition := range conditions {
condition = strings.TrimSpace(condition)
parts := SplitIn(condition, "=", "<", ">")
if len(parts) >= 3 {
// 處理不等於
if strings.HasSuffix(parts[0], "!") {
stringBuilder.Append("`" + strings.Trim(parts[0], "` !") + "` !")
} else {
stringBuilder.Append("`" + strings.Trim(parts[0], "` ") + "`")
}
stringBuilder.Append(strings.TrimSpace(parts[1]))
if len(parts) > 3 {
// 判斷是不是 ">="或"<="
if strings.ContainsAny(parts[2], "= & < & >") {
stringBuilder.Append(strings.TrimSpace(parts[2]))
}
start := len(parts[0]) + len(parts[1]) + 1
this.whereVal = append(this.whereVal, strings.TrimSpace(condition[start:]))
} else {
this.whereVal = append(this.whereVal, strings.TrimSpace(parts[2]))
}
stringBuilder.Append("?")
} else {
tmp := strings.ToUpper(parts[0])
if tmp == "OR" || tmp == "AND" {
stringBuilder.Append(" ").Append(tmp).Append(" ")
} else {
// 處理"in"語句(TODO:用正則處理?)
if strings.ContainsAny(strings.ToLower(parts[0]), "in & ( & )") {
ins := Split(parts[0], "(", ")")
if len(ins) == 3 {
inVals := strings.Split(ins[1], ",")
for _, inVal := range inVals {
this.whereVal = append(this.whereVal, inVal)
}
// in中有多少個值
inLen := len(inVals)
qms := strings.Repeat("?,", inLen)
field := ins[0][:len(ins[0])-3]
stringBuilder.Append("`" + strings.Trim(field, "` ") + "` in").Append("(").Append(qms[:len(qms)-1]).Append(")")
}
} else {
stringBuilder.Append("`" + strings.Trim(parts[0], "` ") + "`")
}
}
}
}
this.where = stringBuilder.String()
}
示例6: GetPageHtml
//bug: next page is always same for popular/last/ective and /list/brief
// 構造分頁html
// curPage 當前頁碼;total總記錄數; PAGE_NUM 每頁顯示多少條
func GetPageHtml(curPage, total, PAGE_NUM int) string {
// 總頁數
pageCount := total / PAGE_NUM
if total%PAGE_NUM != 0 {
pageCount++
}
if pageCount < 2 {
return ""
}
// 顯示5頁,然後顯示...,接著顯示最後兩頁
stringBuilder := util.NewBuffer()
stringBuilder.Append(`<li class="prev previous_page">`)
// 當前是第一頁
if curPage != 1 {
stringBuilder.Append(`<a href="/topics?p=` + strconv.Itoa(curPage-1) + `">← 上一頁</a>`)
}
stringBuilder.Append(`</li>`)
before := 5
showPages := 8
for i := 0; i < pageCount; i++ {
logger.Traceln(i, pageCount)
if i >= showPages {
break
}
if curPage == i+1 {
stringBuilder.Append(`<li class="active"><a href="/topics?p=`).AppendInt(i + 1).Append(`">`).AppendInt(i + 1).Append("</a></li>")
continue
}
// 分界點
if curPage >= before {
if curPage >= 7 {
before = 2
} else {
before = curPage + 2
}
showPages += 2
}
if i == before {
stringBuilder.Append(`<li class="disabled"><a href="#"><span class="gap">…</span></a></li>`)
continue
}
stringBuilder.Append(`<li><a href="/topics?p=`).AppendInt(i + 1).Append(`">`).AppendInt(i + 1).Append("</a></li>")
}
stringBuilder.Append(`<li class="next next_page ">`)
// 最後一頁
if curPage < pageCount {
stringBuilder.Append(`<a href="/topics?p=` + strconv.Itoa(curPage+1) + `">下一頁 →</a>`)
}
stringBuilder.Append(`</li>`)
return stringBuilder.String()
}
示例7: Post
func (this *SolrClient) Post() error {
stringBuilder := util.NewBuffer().Append("{")
needComma := false
for _, addCommand := range this.addCommands {
commandJson, err := json.Marshal(addCommand)
if err != nil {
continue
}
if stringBuilder.Len() == 1 {
needComma = false
} else {
needComma = true
}
if needComma {
stringBuilder.Append(",")
}
stringBuilder.Append(`"add":`).AppendBytes(commandJson)
}
if stringBuilder.Len() == 1 {
logger.Errorln("post docs:no right addcommand")
return errors.New("no right addcommand")
}
stringBuilder.Append("}")
resp, err := http.Post(config.Config["engine_url"]+"/update?wt=json&commit=true", "application/json", stringBuilder)
if err != nil {
logger.Errorln("post error:", err)
return err
}
defer resp.Body.Close()
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
logger.Errorln("parse response error:", err)
return err
}
return nil
}
示例8: updateSetClause
func updateSetClause(form url.Values, fields []string) (query string, args []interface{}) {
stringBuilder := util.NewBuffer()
for _, field := range fields {
if _, ok := form[field]; !ok {
continue
}
stringBuilder.Append(",").Append(field).Append("=?")
args = append(args, form.Get(field))
}
if stringBuilder.Len() > 0 {
query = stringBuilder.String()[1:]
}
return
}
示例9: String
func (this *User) String() string {
buffer := util.NewBuffer()
buffer.Append(this.Username).Append(this.Email).AppendInt(this.Uid).Append(this.Mtime)
return buffer.String()
}
示例10: GenPageHtml
// 構造分頁html(new)
// curPage 當前頁碼;pageNum 每頁記錄數;total總記錄數;uri 當前uri
func GenPageHtml(curPage, pageNum, total int, uri string) string {
// 總頁數
pageCount := total / pageNum
if total%pageNum != 0 {
pageCount++
}
if pageCount < 2 {
return ""
}
needQues := true
if strings.Contains(uri, "?") {
needQues = false
}
// 顯示5頁,然後顯示...,接著顯示最後兩頁
stringBuilder := util.NewBuffer()
// 當前是第一頁
if curPage != 1 {
stringBuilder.Append(`<li><a href="`).Append(uri)
if needQues {
stringBuilder.Append("?")
} else {
stringBuilder.Append("&")
}
stringBuilder.Append("p=").AppendInt(curPage - 1).Append(`">«</a>`)
} else {
stringBuilder.Append(`<li class="disabled"><a href="#">«</a>`)
}
stringBuilder.Append(`</li>`)
before := 5
showPages := 8
for i := 0; i < pageCount; i++ {
if i >= showPages {
break
}
if curPage == i+1 {
stringBuilder.Append(`<li class="active"><a href="`).Append(uri)
if needQues {
stringBuilder.Append("?")
} else {
stringBuilder.Append("&")
}
stringBuilder.Append("p=").AppendInt(i + 1).Append(`">`).AppendInt(i + 1).Append("</a></li>")
continue
}
// 分界點
if curPage >= before {
if curPage >= 7 {
before = 2
} else {
before = curPage + 2
}
showPages += 2
}
if i == before {
stringBuilder.Append(`<li class="disabled"><a href="#"><span class="gap">…</span></a></li>`)
continue
}
stringBuilder.Append(`<li><a href="`).Append(uri)
if needQues {
stringBuilder.Append("?")
} else {
stringBuilder.Append("&")
}
stringBuilder.Append("p=").AppendInt(i + 1).Append(`">`).AppendInt(i + 1).Append("</a></li>")
}
// 最後一頁
if curPage < pageCount {
stringBuilder.Append(`<li><a href="`).Append(uri)
if needQues {
stringBuilder.Append("?")
} else {
stringBuilder.Append("&")
}
stringBuilder.Append("p=").AppendInt(curPage + 1).Append(`">»</a>`)
} else {
stringBuilder.Append(`<li class="disabled"><a href="#">»</a>`)
}
stringBuilder.Append(`</li>`)
return stringBuilder.String()
}