1:data/data.go
Package DataImport ("FMT" "Database/sql" _"GITHUB.COM/LIB/PQ")Const(Host="192.168.72.128"Port=5432User="Test"Password="Test"dbname="TestDB")varDb *SQL. Dbfunc init () {varErr error Pginfo:= Fmt. Sprintf ("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) Db, Err= SQL. Open ("Postgres", Pginfo)ifErr! =nil{Panic (Err)}}
2:post/post.go
Package Postimport (."Chapter10/data") Type Poststruct{IDint' JSON:"ID"' Contentstring' JSON:"content"' Authorstring' JSON:"author"'}func (post*Post) Addpost () (err error) {sql:="insert into post (Content,author) VALUES ($1,$2) returning ID"stmt, err:=db.prepare (SQL)ifErr! =nil{return} defer stmt. Close () Err= stmt. Queryrow (post. Content, Post. Author). Scan (&post.id)return}func (Post*Post) Delpost () (err error) {sql:="Delete from post where id=$1"_,err=db.exec (SQL, post.id)return}func (Post*Post) Editpost () (err error) {sql:="Update post set content=$1 where Id=$2"_,err=db.exec (SQL, post. Content, Post.id)return}func findpost (IDint) (Err error, post post) {sql:="Select ID, content, author from post where id=$1"Post=post{} Err= Db.queryrow (sql, id). Scan (&post.id, &post. Content, &Post. Author)return}
3:main.go
Package Mainimport ("Encoding/json" "net/http" "Path" "StrConv" " Time" ."Chapter10/post") Type Resultstruct{Noint' JSON:"No"' MSGstring' JSON:"msg"' OBJInterface{} ' JSON:"Obj,omitempty"' time. Time ' JSON:"Response_time"'}func main () {server:=http. server{Addr:"127.0.0.1:8080",} http. Handlefunc ("/post/", handlerrequest) server. Listenandserve ()}func handlerrequest (w http. Responsewriter, R*http. Request) {varErr ErrorSwitchr.method{ Case "GET": Err=Handleget (W, R) Case "POST": Err=Handlepost (W, R) Case "PUT": Err=Handleput (W, R)}ifErr! =nil{http. Error (W, err. Error (), HTTP. Statusinternalservererror)}}func Handleget (w http. Responsewriter, R*http. Request) (err error) {ID, err:=StrConv. Atoi (path. Base (R.url. Path))ifErr! =nil{return} err,post:=findpost (ID)ifErr! =nil{return } varresult =result{No: -, MSG:"get post Information", Obj:post, Time:time. Now (),} output, err:= json. Marshal (&result)ifErr! =nil{return} w.header (). Set ("Content-type","Application/json") w.write (output)return}func handlepost (w http. Responsewriter, R*http. Request) (err error) {len:=r.contentlength Body:= Make ([]byte, Len) r.body.read (Body)varPost post JSON. Unmarshal (Body,&post) Err=Post. Addpost ()ifErr! =nil{return } varresult =result{No: -, MSG:"Save post Information", Time:time. Now (),} output, err:= json. Marshal (&result)ifErr! =nil{return} w.header (). Set ("Content-type","Application/json") w.write (output)return} func handleput (w http. Responsewriter, R*http. Request) (err error) {len:=r.contentlength Body:= Make ([]byte, Len) r.body.read (Body)varPost post JSON. Unmarshal (Body,&post) Err=Post. Editpost ()ifErr! =nil{return } varresult =result{No: -, MSG:"Modify post Information", Time:time. Now (),} output, err:= json. Marshal (&result)ifErr! =nil{return} w.header (). Set ("Content-type","Application/json") w.write (output)return}
014-go Web to PG additions and deletions test