项目中需要mock接口,如果使用代码生成json需要先把结构体这些定义好,返回的结构如果经常变化就得经常修改代码然后编译发布,使用静态json文件代替会方便很多,话不多说,上码:
package main
import (
"net/http"
"os"
)
type HTMLDir struct {
d http.Dir
}
func main() {
fs := http.FileServer( HTMLDir{http.Dir("public/") } )
http.Handle("/", http.StripPrefix("/", fs))
http.ListenAndServe(":8000", nil)
}
func (d HTMLDir ) Open(name string) (http.File, error){
f, err := d.d.Open(name)
if os.IsNotExist(err) {
// Not found, try with .html
if f, err := d.d.Open(name + ".json"); err == nil {
return f, nil
}
}
return f, err
}
本文链接:https://iokde.com/post/go-static-json.html,参与评论 »
--EOF--
发表于 2021-01-08 11:10:00。
本站使用「署名 4.0 国际」创作共享协议,转载请注明作者及原网址。tools更多说明 »
提醒:本文最后更新于 1243 天前,文中所描述的信息可能已发生改变,请谨慎使用。
Comments