当前位置: 代码迷 >> 综合 >> [golang]文件服务器
  详细解决方案

[golang]文件服务器

热度:42   发布时间:2023-12-15 22:46:26.0

    也来凑个热闹,学习Golang,摆脱苦逼的C++,上代码一个文件服务器。

package mainimport ("os""os/exec""strings""net/http"
)func main(){h := http.FileServer(http.Dir(getCurrentPath()))http.ListenAndServe(":8080", h)
}func getCurrentPath() string {s, err := exec.LookPath(os.Args[0])checkErr(err)i := strings.LastIndex(s, "\\")path := string(s[0 : i+1])return path
}func checkErr(err error) {if err != nil {panic(err)}
}
在浏览器,输入:localhost:8080 就能看效果了,确实简单