package main
import (
"fmt"
"log"
"net/http"
)
func formHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
fmt.Fprintf(w, "POST Request successful")
name := r.FormValue("name")
address := r.FormValue("address")
fmt.Fprintf(w, "name = %s\n", name)
fmt.Fprintf(w, "address = %s\n", address)
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hello" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "method is not supported", http.StatusMethodNotAllowed)
return
}
fmt.Fprintf(w, "Hello")
}
func main() {
// 静态资源
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)
fmt.Printf("listening on port 8080... \n")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
上一篇
构建简单crud(没有操作数据库)
go mod init movie_crud
go get "github.com/gorilla/mux"
package main
import (
"encoding/json"
"fmt"
"log"
2023-04-30
下一篇
go使用fiber实现CRM
installgo get -u gorm.io/gorm
go get -u gorm.io/driver/sqlite
技术选型: gorm(sqlite),go-fiber
开发
database/database.gopack
2023-04-30