golang 似乎回到servlet的时代

前端之家收集整理的这篇文章主要介绍了golang 似乎回到servlet的时代前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

servlet,需要tomcat,需要在web.xml里面配置,然后写一个类;golang写一个服务器,一个映射,一个对应的函数


package main import ( "database/sql" "fmt" _ "github.com/mattn/go-sqlite3" "html/template" "log" "net/http" "strconv" ) type User struct { ID int Name string } func (user User) String() string { format := `ID : %d Name: %s` return fmt.Sprintf(format,user.ID,user.Name) } func (user User) Save() { db,err := sql.Open("sqlite3","./foo.db") if err != nil { log.Fatal(err) } defer db.Close() log.Println("insert into users(userId,uname) values('%d,%s'),user.Name") //db.Exec("insert into users(userId,uname) values(4,'test')") db.Exec(fmt.Sprintf(`insert into users(userId,uname) values('%d','%s')`,user.Name)) if err != nil { log.Fatal(err) } } func saveUser(w http.ResponseWriter,req *http.Request) { userId,_ := strconv.Atoi(req.FormValue("Id")) name := req.FormValue("name") out := strconv.Itoa(userId) + "-" + name u := &User{ID: userId,Name: name} log.Println(out,";",u.String()) u.Save() //fmt.Fprintf(w,u.String()) http.Redirect(w,req,"/",http.StatusFound) } func addUser(w http.ResponseWriter,r *http.Request) { t,_ := template.ParseFiles("tmpl/addUser.html") t.Execute(w,nil) } func getUsers(w http.ResponseWriter,r *http.Request) { db,"./foo.db") if err != nil { log.Fatal(err) } defer db.Close() rows,err := db.Query("select * from users") if err != nil { log.Fatal(err) } defer rows.Close() var id,name string users := "" for rows.Next() { err = rows.Scan(&id,&name) if err == nil { log.Println(id,name) users = users + id + "," + name + ";\n" } } fmt.Fprintf(w,users) } func delUser(w http.ResponseWriter,r *http.Request) { fmt.Fprintf(w,"hello world!") } func updateUser(w http.ResponseWriter,"hello world!") } func sayHello(w http.ResponseWriter,"hello world!") } func main() { http.HandleFunc("/",sayHello) http.HandleFunc("/user",addUser) http.HandleFunc("/users",getUsers) http.HandleFunc("/saveuser",saveUser) http.HandleFunc("/deluser",delUser) http.HandleFunc("/updateuser",updateUser) err := http.ListenAndServe(":8880",nil) if err != nil { log.Fatal("ListenAndServe: ",err.Error()) } }

原文链接:/go/190277.html

猜你在找的Go相关文章