Go实现最简单的在线视频播放 发表于 2020-04-28 | 分类于 程序人生 > Golang 本文字数: 621 | 阅读时长 ≈ 1 分钟 如下: 123456789101112131415161718192021222324252627package mainimport ( "log" "net/http" "os" "time")func video(response http.ResponseWriter, request *http.Request) { response.Header().Set("Content-Type", "video/mp4") file, err := os.Open("/Users/sherlockgy/Downloads/1.mp4") if err != nil { log.Println("文件打开异常...") return } http.ServeContent(response, request, "test", time.Now(), file) defer file.Close()}func main() { http.HandleFunc("/video", video) // 设置访问的路由 err := http.ListenAndServe(":9090", nil) // 设置监听的端口 if err != nil { log.Fatal("ListenAndServe: ", err) }}