package main
import (
//"database/sql"
//_ "github.com/mattn/go-sqlite3"
"fmt"
"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 addUser(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)
fmt.Fprintf(w, u.String())
}
func sayHello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world!")
}
func main() {
http.HandleFunc("/", sayHello)
http.HandleFunc("/user", addUser)
err := http.ListenAndServe(":8880", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err.Error())
}
}
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)