go+postgresql服务器

Posted dy blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go+postgresql服务器相关的知识,希望对你有一定的参考价值。

技术分享
  1 func AddGroup(w http.ResponseWriter, req *http.Request) {
  2 
  3     outputReq(req)
  4 
  5     responseJson := map[string]interface{}{jsonKeyMessage: "", jsonKeyResult: nil}
  6 
  7     initResponse(w)
  8     err := req.ParseForm()
  9     if err != nil {
 10         writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError)
 11         return
 12     }
 13 
 14     nickname := toString(req.Form["nickname"])
 15     delete(req.Form, "nickname")
 16     icon := toString(req.Form["icon"])
 17     delete(req.Form, "icon")
 18 
 19     requestJson := initRequestJson(req.Form)
 20     customInfo, err := json.Marshal(requestJson)
 21     if err != nil {
 22         writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError)
 23         return
 24     }
 25 
 26     db, err := openSql()
 27     defer db.Close()
 28     if err != nil {
 29         writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError)
 30         return
 31     }
 32 
 33     tx, err := db.Begin()
 34     if err != nil {
 35         writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError)
 36         return
 37     }
 38 
 39     stmt, err := tx.Prepare(`
 40     insert into objects(nickname,icon,custom,isUser)
 41     values ($1,$2,$3,false) returning id
 42     `)
 43     if err != nil {
 44         writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError)
 45         return
 46     }
 47 
 48     rows, err := stmt.Query(nickname, icon, customInfo)
 49     if err != nil {
 50         errString := err.Error() + " " + errorString(tx.Rollback())
 51         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
 52         return
 53     }
 54 
 55     if !rows.Next() {
 56         errString := "add group failed! " + errorString(tx.Rollback())
 57         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
 58         return
 59     }
 60 
 61     var groupID int64
 62     err = rows.Scan(&groupID)
 63     ///fmt.Println("groupID:", +groupID)
 64     if err != nil {
 65         errString := err.Error() + " " + errorString(tx.Rollback())
 66         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
 67         return
 68     }
 69 
 70     err = rows.Close()
 71     if err != nil {
 72         errString := err.Error() + " " + errorString(tx.Rollback())
 73         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
 74         return
 75     }
 76     err = stmt.Close()
 77     if err != nil {
 78         errString := err.Error() + " " + errorString(tx.Rollback())
 79         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
 80         return
 81     }
 82     stmt, err = tx.Prepare("insert into groups(id,createTime) values($1,now())")
 83     defer stmt.Close()
 84     if err != nil {
 85         errString := err.Error() + " " + errorString(tx.Rollback())
 86         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
 87         return
 88     }
 89 
 90     result, err := stmt.Exec(groupID)
 91     //rows1, err := tx.Exec("insert into groups(id,createTime) values(" + fmt.Sprint(groupID) + ",now())")
 92     if err != nil {
 93         errString := err.Error() + " " + errorString(tx.Rollback())
 94         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
 95         return
 96     }
 97 
 98     affect, err := result.RowsAffected()
 99     if err != nil {
100         errString := err.Error() + " " + errorString(tx.Rollback())
101         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
102         return
103     }
104     if affect != 1 {
105         errString := "add group failed! " + errorString(tx.Rollback())
106         writeResponse(errString, responseJson, w, http.StatusInternalServerError)
107         return
108     }
109 
110     _, err = tx.Exec("commit")
111     if err != nil {
112         writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError)
113         return
114     }
115     responseJson[jsonKeyResult] = fmt.Sprint(groupID)
116     writeResponse("success", responseJson, w, http.StatusOK)
117 }
postgresql事务

 

以上是关于go+postgresql服务器的主要内容,如果未能解决你的问题,请参考以下文章

解决go: go.mod file not found in current directory or any parent directory; see ‘go help modules‘(代码片段

PostgreSQL 是不是支持表(片段)的透明压缩?

你知道的Go切片扩容机制可能是错的

golang代码片段(摘抄)

golang基础-Postgresql-ORM框架github.com/go-pg/pg学习五(实战)

golang基础-Postgresql-ORM框架github.com/go-pg/pg学习五(实战)