Go标准库中是否有一个函数可以将缓冲区编码为go中的任何图像格式

I'm writing a micro-service with some simple image manipulation in go.

When I come to sending the manipulated image back to the user I have to encode it correctly before writing it via a buffer.

I can detect the format easily enough, so it's not precisely a problem. Currently I use my format string and do it like this:

buffer := new(bytes.Buffer)

switch format { //format is just a string as returned by image.Decode()
case "jpeg":
    err := jpeg.Encode(buffer, img, nil) //img is just an image.Image 
    if err != nil {
        //Do some error handling
    }
case "png":
    err := png.Encode(buffer, img)
    if err != nil {
        //Do some error handling
    }
//and so on...

Now this works just fine, I was about to split it out into my own function when I started to think I must have missed something here. It seems too obvious to not have a function like image.Encode(buffer, image, format).

It's not exactly hassle to write it, but my code will soon become unnecessarily unwieldy if I start re-implementing core language functionality.

If it's just not there because go is a nice tidy language with a small footprint, I'm cool with that.

The standard library does not contain a function for encoding an image to an arbitrary format.

One reason that the library does not have such a function is that there are different encoding options for each image format. For example the JPEG options and PNG options are different.