这个记事本程序只是简易的实现,并没有实现前端的html界面内容,也没有使用框架,只是为了熟练GO语言Web操作而进行的小练习。
首先在Mysql数据库建名为test的数据库,名为note的数据表,字段有三个,noteid,notename,content.
go语言数据库引擎使用go-sql-driver.进行安装
go get -u github.com/go-sql-driver/mysql
注意使用go get 之前确保电脑装了Git.没有请自行安装
下载postman接口测试工具,安装。
一切就绪之后,写完代码进行测试。
代码:
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"net/http"
"strings"
)
func checkErr(err error){
if err!=nil{
log.Fatal("panic occured:",err)
panic(err)
}
}
type Note struct{
Noteid int
Notename string
Content string
}
func searchHanler(w http.ResponseWriter,r *http.Request){
r.ParseForm() //解析
fmt.Println(r.Form)//输出到服务器端的打印信息,map类型 type Values map[string][]string
//打开数据库操作
db,err :=sql.Open("mysql","root:root123@/test")
checkErr(err)
defer db.Close()
for _,v :=range r.Form{
fmt.Println("val:",strings.Join(v,""))
//查询数据
for i:=0;i<len(v);i++{
search_query:="select * from note where noteid="+v[i]+";"
fmt.Println(search_query)
rows,err :=db.Query(search_query)
checkErr(err)
for rows.Next(){
var noteid int
var notename string
var content string
err=rows.Scan(¬eid,¬ename,&content)
checkErr(err)
//将这一步改成JSON字符串传输至前面页面
note :=Note{
Noteid:noteid,
Notename: notename,
Content: content,
}
b,err :=json.Marshal(note)
checkErr(err)
fmt.Fprintf(w,string(b))
//fmt.Fprintf(w,noteid+" "+notename+" "+content)
}
}
}
}
type Gonote struct {
Notename string
Content string
}
type Message struct{
Message string
}
func AddHanler(w http.ResponseWriter,r *http.Request) {
//获取请求报文的内容长度
len :=r.ContentLength
//新建一个字节切片,长度与请求报文的内容长度相同
body :=make([]byte,len)
//读取r的请求主体,并将具体内容读入body中
r.Body.Read(body)
var n Gonote
err :=json.Unmarshal(body,&n)
checkErr(err)
fmt.Println(n.Notename,n.Content)
//打开数据库操作
db, err := sql.Open("mysql", "root:root123@/test")
checkErr(err)
defer db.Close()
//插入数据
stmt,err:=db.Prepare("INSERT note SET notename=?,content=?")
checkErr(err)
res,err :=stmt.Exec(n.Notename,n.Content)
checkErr(err)
id,err :=res.LastInsertId()
checkErr(err)
fmt.Println("Last InsertId :",id)
m:=Message{
Message: "Insert Success!",
}
b,err:=json.Marshal(m)
checkErr(err)
fmt.Fprintf(w,string(b))
//fmt.Fprintln(w,"insert success!")
}
type NoteModify struct{
Noteid int
Content string
}
func ModifyHanler(w http.ResponseWriter,r *http.Request){
//获取请求报文的内容长度
len :=r.ContentLength
//新建一个字节切片,长度与请求报文的内容长度相同
body :=make([]byte,len)
//读取r的请求主体,并将具体内容读入body中
r.Body.Read(body)
var nm NoteModify
err :=json.Unmarshal(body,&nm)
checkErr(err)
fmt.Println(nm.Noteid,nm.Content)
//打开数据库操作
db, err := sql.Open("mysql", "root:root123@/test")
checkErr(err)
defer db.Close()
//更新数据
stmt,err:=db.Prepare("UPDATE note SET content=? where noteid=?")
checkErr(err)
res,err :=stmt.Exec(nm.Content,nm.Noteid)
checkErr(err)
affect,err :=res.RowsAffected()
checkErr(err)
fmt.Println("Affect rows :",affect)
m:=Message{
Message: "Modify Success!",
}
b,err:=json.Marshal(m)
checkErr(err)
fmt.Fprintf(w,string(b))
//fmt.Fprintln(w,"modify success!")
}
func DeleteHanler(w http.ResponseWriter,r *http.Request){
r.ParseForm() //解析
fmt.Println(r.Form)//输出到服务器端的打印信息,map类型 type Values map[string][]string
//打开数据库操作
db,err :=sql.Open("mysql","root:root123@/test")
checkErr(err)
defer db.Close()
for _,v :=range r.Form{
//fmt.Println("val:",strings.Join(v,""))
//删除数据
for i:=0;i<len(v);i++{
delete_query:="delete from note where noteid=?"
stmt,err :=db.Prepare(delete_query)
checkErr(err)
res,err:=stmt.Exec(v[i])
checkErr(err)
affect,err :=res.RowsAffected()
checkErr(err)
fmt.Println("affect rows:",affect)
if affect!=0{
m:=Message{
Message: "Delete Success!",
}
b,err:=json.Marshal(m)
checkErr(err)
fmt.Fprintf(w,string(b))
//fmt.Fprintln(w,"Delete success! ")
}else{
m:=Message{
Message: "The id not exist!",
}
b,err:=json.Marshal(m)
checkErr(err)
fmt.Fprintf(w,string(b))
//fmt.Fprintf(w,"The id not exist!")
}
}
}
}
func main(){
//mux :=http.NewServeMux()
http.HandleFunc("/search",searchHanler)
http.HandleFunc("/Add",AddHanler)
http.HandleFunc("/Modify",ModifyHanler)
http.HandleFunc("/Delete",DeleteHanler)
err :=http.ListenAndServe(":9090",nil)
if err!=nil{
log.Fatal("panic occur:",err)
}
}