蓝山帝景
如果PDF过大,一次性读取会有内存问题。另外,不要使用panic来中断逻辑运行,一旦panic没被处理,整个程序挂掉funcmain(){http.HandleFunc("/pdf",function(wResponseWriter,r*http.Request){w.Header().Add("Content-Type","application/pdf");fp,err:=os.Open("./go.pdf")iferr!=nil{http.Error(w,fmt.Errorf("internalservererror"),http.StatusInternalServerError)return;}deferfp.Close()if_,err:=io.Copy(w,fp);err!=nil{http.Error(w,fmt.Errorf("internalservererror"),http.StatusInternalServerError)return;}});}
0
0