第一个函数将图像直接写入本地。
第二个是我的目标;来获取返回值,我可以将其传递给本地或云存储写入器。
保存到Google Cloud Storage功能需要io。Reader使用io.Copy.
func makeCatImage() {
myImg := image.NewRGBA(image.Rect(0, 0, 12, 6))
out, _ := os.Create("cat.png")
_ = png.Encode(out, myImg)
_ = out.Close()
}
func getCatImage() someReturnValue {
myImg := image.NewRGBA(image.Rect(0, 0, 12, 6))
// TODO ...
_ = png.Encode(out, myImg)
return out
}
谁创建谁销毁
func makeCatImageHttp(w http.ResponseWriter) {
//ResponseWriter Close by net/http, you have not create it, need not close it
getCatImage(w)
}
func makeCatImageLocal() {
//Local file must Close you have create it, so close it
out, _ := os.Create("cat.png")
defer out.Close()
getCatImage(out)
}
func getCatImage(w io.Writer) {
myImg := image.NewRGBA(image.Rect(0, 0, 12, 6))
_ = png.Encode(w, myImg)
}