本文整理匯總了Golang中util.MustInt函數的典型用法代碼示例。如果您正苦於以下問題:Golang MustInt函數的具體用法?Golang MustInt怎麽用?Golang MustInt使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了MustInt函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: FindWiki
// 某個wiki頁麵詳細信息
func FindWiki(uri string) map[string]interface{} {
wiki := model.NewWiki()
if err := wiki.Where("uri=" + uri).Find(); err != nil {
logger.Errorln("wiki service FindWiki error:", err)
return nil
}
uids := make(map[int]int)
uids[wiki.Uid] = wiki.Uid
if wiki.Cuid != "" {
cuids := strings.Split(wiki.Cuid, ",")
for _, cuid := range cuids {
tmpUid := util.MustInt(cuid)
uids[tmpUid] = tmpUid
}
}
userMap := getUserInfos(uids)
result := make(map[string]interface{})
util.Struct2Map(result, wiki)
result["user"] = userMap[wiki.Uid]
if wiki.Cuid != "" {
cuids := strings.Split(wiki.Cuid, ",")
cusers := make([]*model.User, len(cuids))
for i, cuid := range cuids {
cusers[i] = userMap[util.MustInt(cuid)]
}
result["cuser"] = cusers
}
return result
}
示例2: FavoriteHandler
// 收藏(取消收藏)
// uri: /favorite/{objid:[0-9]+}.json
func FavoriteHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
user, _ := filter.CurrentUser(req)
if !util.CheckInt(req.PostForm, "objtype") {
fmt.Fprint(rw, `{"ok": 0, "error":"參數錯誤"}`)
return
}
var err error
objtype := util.MustInt(req.PostFormValue("objtype"))
collect := util.MustInt(req.PostFormValue("collect"))
if collect == 1 {
err = service.SaveFavorite(user["uid"].(int), util.MustInt(vars["objid"]), objtype)
} else {
err = service.CancelFavorite(user["uid"].(int), util.MustInt(vars["objid"]), objtype)
}
if err != nil {
fmt.Fprint(rw, `{"ok": 0, "error":"`+err.Error()+`""}`)
return
}
fmt.Fprint(rw, `{"ok": 1, "message":"success"}`)
}
示例3: checkRange
// checkRange 檢查範圍值是否合法。
// src為要檢查的值;destRange為目標範圍;msg出錯時信息參數
func checkRange(src int, destRange string, msg string) (errMsg string) {
parts := strings.SplitN(destRange, ",", 2)
parts[0] = strings.TrimSpace(parts[0])
parts[1] = strings.TrimSpace(parts[1])
min, max := 0, 0
if parts[0] == "" {
max = util.MustInt(parts[1])
if src > max {
errMsg = fmt.Sprintf(msg, max)
}
return
}
if parts[1] == "" {
min = util.MustInt(parts[0])
if src < min {
errMsg = fmt.Sprintf(msg, min)
}
return
}
if min == 0 {
min = util.MustInt(parts[0])
}
if max == 0 {
max = util.MustInt(parts[1])
}
if src < min || src > max {
errMsg = fmt.Sprintf(msg, min, max)
return
}
return
}
示例4: LikeHandler
// 喜歡(或取消喜歡)
// uri: /like/{objid:[0-9]+}.json
func LikeHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
user, _ := filter.CurrentUser(req)
// 入庫
err := service.PostComment(util.MustInt(vars["objid"]), util.MustInt(req.FormValue("objtype")), user["uid"].(int), req.FormValue("content"), req.FormValue("objname"))
if err != nil {
fmt.Fprint(rw, `{"errno": 1, "error":"服務器內部錯誤"}`)
return
}
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
}
示例5: FindToMsgsByUid
// 獲得發給某人的短消息(收件箱)
func FindToMsgsByUid(uid string) []map[string]interface{} {
messages, err := model.NewMessage().Where("to=" + uid + " AND tdel=" + model.TdelNotDel).Order("ctime DESC").FindAll()
if err != nil {
logger.Errorln("message service FindToMsgsByUid Error:", err)
return nil
}
uids := make(map[int]int)
ids := make([]int, 0, len(messages))
for _, message := range messages {
uids[message.From] = message.From
if message.Hasread == model.NotRead {
ids = append(ids, message.Id)
}
}
// 標記已讀
go MarkHasRead(ids, false, util.MustInt(uid))
userMap := getUserInfos(uids)
result := make([]map[string]interface{}, len(messages))
for i, message := range messages {
tmpMap := make(map[string]interface{})
util.Struct2Map(tmpMap, message)
tmpMap["user"] = userMap[message.From]
// 為了跟係統消息一致
tmpMap["title"] = "發來了一條消息:"
result[i] = tmpMap
}
return result
}
示例6: CommentHandler
// 評論(或回複)
// uri: /comment/{objid:[0-9]+}.json
func CommentHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
user, _ := filter.CurrentUser(req)
if !util.CheckInt(req.PostForm, "objtype") {
fmt.Fprint(rw, `{"errno": 1, "error":"參數錯誤"}`)
return
}
// 入庫
comment, err := service.PostComment(user["uid"].(int), util.MustInt(vars["objid"]), req.PostForm)
if err != nil {
fmt.Fprint(rw, `{"errno": 1, "error":"服務器內部錯誤"}`)
return
}
buf, err := json.Marshal(comment)
if err != nil {
logger.Errorln("[RecentCommentHandler] json.marshal error:", err)
fmt.Fprint(rw, `{"errno": 1, "error":"解析json出錯"}`)
return
}
fmt.Fprint(rw, `{"errno": 0, "error":"", "data":`+string(buf)+`}`)
}
示例7: TopicDetailHandler
// 社區帖子詳細頁
// uri: /topics/{tid:[0-9]+}
func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
topic, replies, err := service.FindTopicByTid(vars["tid"])
if err != nil {
util.Redirect(rw, req, "/topics")
return
}
likeFlag := 0
hadCollect := 0
user, ok := filter.CurrentUser(req)
if ok {
uid := user["uid"].(int)
tid := topic["tid"].(int)
likeFlag = service.HadLike(uid, tid, model.TYPE_TOPIC)
hadCollect = service.HadFavorite(uid, tid, model.TYPE_TOPIC)
}
service.Views.Incr(req, model.TYPE_TOPIC, util.MustInt(vars["tid"]))
// 設置內容模板
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html,/template/common/comment.html")
// 設置模板數據
filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies, "likeflag": likeFlag, "hadcollect": hadCollect})
}
示例8: NewTopicHandler
// 新建帖子
// uri: /topics/new{json:(|.json)}
func NewTopicHandler(rw http.ResponseWriter, req *http.Request) {
nodes := genNodes()
vars := mux.Vars(req)
title := req.FormValue("title")
// 請求新建帖子頁麵
if title == "" || req.Method != "POST" || vars["json"] == "" {
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html")
filter.SetData(req, map[string]interface{}{"nodes": nodes})
return
}
user, _ := filter.CurrentUser(req)
// 入庫
topic := model.NewTopic()
topic.Uid = user["uid"].(int)
topic.Nid = util.MustInt(req.FormValue("nid"))
topic.Title = req.FormValue("title")
topic.Content = req.FormValue("content")
errMsg, err := service.PublishTopic(topic)
if err != nil {
fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
return
}
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
}
示例9: 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
}
示例10: initMaxOnlineNum
func initMaxOnlineNum() {
maxRwMu.Lock()
defer maxRwMu.Unlock()
if maxOnlineNum == 0 {
data, err := ioutil.ReadFile(getDataFile())
if err != nil {
logger.Errorln("read data file error:", err)
return
}
maxOnlineNum = util.MustInt(strings.TrimSpace(string(data)))
}
}
示例11: LikeHandler
// 喜歡(或取消喜歡)
// uri: /like/{objid:[0-9]+}.json
func LikeHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
user, _ := filter.CurrentUser(req)
if !util.CheckInt(req.PostForm, "objtype") || !util.CheckInt(req.PostForm, "flag") {
fmt.Fprint(rw, `{"ok": 0, "error":"參數錯誤"}`)
return
}
uid := user["uid"].(int)
objid := util.MustInt(vars["objid"])
objtype := util.MustInt(req.PostFormValue("objtype"))
likeFlag := util.MustInt(req.PostFormValue("flag"))
err := service.LikeObject(uid, objid, objtype, likeFlag)
if err != nil {
fmt.Fprint(rw, `{"ok": 0, "error":"服務器內部錯誤"}`)
return
}
fmt.Fprint(rw, `{"ok": 1, "msg":"success", "data":""}`)
}
示例12: NodesHandler
// 某節點下的帖子列表
// uri: /topics/node{nid:[0-9]+}
func NodesHandler(rw http.ResponseWriter, req *http.Request) {
page, _ := strconv.Atoi(req.FormValue("p"))
if page == 0 {
page = 1
}
vars := mux.Vars(req)
topics, total := service.FindTopics(page, 0, "nid="+vars["nid"])
pageHtml := service.GetPageHtml(page, total)
// 當前節點信息
node := model.GetNode(util.MustInt(vars["nid"]))
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/node.html")
// 設置模板數據
filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topics": topics, "page": template.HTML(pageHtml), "total": total, "node": node})
}
示例13: doCrawl
func doCrawl(wbconf map[string]string, isAll bool) {
crawlUrl := wbconf["incr_url"]
if isAll {
crawlUrl = wbconf["all_url"]
}
listselector := wbconf["listselector"]
resultselector := wbconf["resultselector"]
pageField := wbconf["page_field"]
maxPage := 1
if isAll {
maxPage = util.MustInt(wbconf["max_page"])
}
// 個人博客,一般通過 tag 方式獲取,這種處理方式和搜索不一樣
if wbconf["keywords"] == "" {
for p := maxPage; p >= 1; p-- {
if pageField == "" {
// 標題不包含 go 等關鍵詞的,也入庫
if err := parseArticleList(crawlUrl+strconv.Itoa(p), listselector, resultselector, false); err != nil {
break
}
}
}
return
}
keywords := strings.Split(wbconf["keywords"], ",")
for _, keyword := range keywords {
for p := 1; p <= maxPage; p++ {
page := fmt.Sprintf("&%s=%d", pageField, p)
if err := parseArticleList(crawlUrl+keyword+page, listselector, resultselector, true); err != nil {
logger.Errorln("parse article url error:", err)
break
}
}
}
}
示例14: SendMessageHandler
// 發短消息
// uri: /message/send{json:(|.json)}
func SendMessageHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
content := req.FormValue("content")
// 請求新建帖子頁麵
if content == "" || req.Method != "POST" || vars["json"] == "" {
user := service.FindUserByUsername(req.FormValue("username"))
filter.SetData(req, map[string]interface{}{"user": user})
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/messages/send.html")
return
}
user, _ := filter.CurrentUser(req)
to := util.MustInt(req.FormValue("to"))
success := service.SendMessageTo(user["uid"].(int), to, content)
if !success {
fmt.Fprint(rw, `{"errno": 1, "error":"對不起,發送失敗,請稍候再試!"}`)
return
}
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
}
示例15: NewTopicHandler
// 新建帖子
// uri: /topics/new{json:(|.json)}
func NewTopicHandler(rw http.ResponseWriter, req *http.Request) {
nodes := genNodes()
vars := mux.Vars(req)
content := req.FormValue("content")
// 請求新建帖子頁麵
if content == "" || req.Method != "POST" || vars["json"] == "" {
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html")
filter.SetData(req, map[string]interface{}{"nodes": nodes})
return
}
// 入庫
topic := model.NewTopic()
logger.Traceln("anonymous")
logger.Traceln(req.FormValue("anonymous"))
if req.FormValue("anonymous") == "1" {
topic.Uid, _ = strconv.Atoi(Config["auid"])
} else {
user, _ := filter.CurrentUser(req)
if user != nil {
topic.Uid = user["uid"].(int)
} else {
topic.Uid, _ = strconv.Atoi(Config["auid"])
}
}
logger.Traceln(topic)
topic.Nid = util.MustInt(req.FormValue("nid"))
topic.Title = req.FormValue("title")
topic.Content = req.FormValue("content")
logger.Traceln(topic)
errMsg, err := service.PublishTopic(topic)
logger.Traceln("PublishTopic end")
logger.Traceln(errMsg)
if err != nil {
fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
return
}
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
}