- 将原始main函数中的HTTP处理逻辑拆分到独立文件 - 新增main1.go实现基础HTTP服务器功能 - 新增main2.go增强路由处理和重定向逻辑 - 新增main3.go使用标准库处理程序优化代码结构 - 更新main.go调用新的main3函数并注释其他入口点 - 移除main.go中旧的HTTP处理相关导入和类型定义
27 lines
606 B
Go
27 lines
606 B
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type StringHandle3 struct {
|
|
message string
|
|
}
|
|
|
|
func (sh *StringHandle3) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
|
Printfln("Request for %v", request.URL.Path)
|
|
io.WriteString(writer, sh.message)
|
|
}
|
|
|
|
func main3() {
|
|
http.Handle("/message", &StringHandle3{message: "Hello World!"})
|
|
http.Handle("/favicon.ico", http.NotFoundHandler())
|
|
http.Handle("/", http.RedirectHandler("/message", http.StatusTemporaryRedirect))
|
|
err := http.ListenAndServe("localhost:5000", nil)
|
|
if err != nil {
|
|
Printfln("ListenAndServe Error: %v", err)
|
|
return
|
|
}
|
|
}
|