1 Star 1 Fork 0

湖底观景/GolangTraining

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 845 Bytes
一键复制 编辑 原始数据 按行查看 历史
GoesToEleven 提交于 2016-04-20 17:45 . changes dir structure
package main
import (
"io"
"net/http"
"os"
"strings"
)
func upTown(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/html; charset=utf-8")
var dogName string
fs := strings.Split(req.URL.Path, "/")
if len(fs) >= 2 {
dogName = fs[1]
}
// the image doesn't serve
io.WriteString(res, `
<h1>Dog Name: `+dogName+`</h1><br>
<img src="/toby.jpg">
`)
}
func dogPic(res http.ResponseWriter, req *http.Request) {
f, err := os.Open("toby.jpg")
if err != nil {
http.Error(res, "file not found", 404)
return
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
http.Error(res, "file not found", 404)
return
}
http.ServeContent(res, req, f.Name(), fi.ModTime(), f)
}
func main() {
http.HandleFunc("/", upTown)
http.HandleFunc("/toby.jpg", dogPic)
http.ListenAndServe(":9000", nil)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjianGood/GolangTraining.git
git@gitee.com:zhangjianGood/GolangTraining.git
zhangjianGood
GolangTraining
GolangTraining
afa19f5c43f3

搜索帮助