Go's standard library gives you solid image support and only basic audio support. The image package and its subpackages (image/png, image/jpeg, image/gif, image/draw, image/color) decode, transform and encode pictures, while encoding/binary and os let you read and write raw audio such as WAV files. There is no standard decoder for MP3 or other compressed audio, and no resizing function in the standard library, so you add modules such as golang.org/x/image/draw for scaling.
The sections below show practical image and audio code for current Go, and when to reach for a module or an external tool.
| Task | Standard library | Beyond it |
|---|---|---|
| Decode and encode PNG, JPEG, GIF | image/png, image/jpeg, image/gif | WebP and TIFF in golang.org/x/image |
| Pixel access and colour models | image, image/color | None |
| Compositing and drawing | image/draw | Text needs golang.org/x/image/font |
| Resizing | Not included | golang.org/x/image/draw |
| WAV audio | encoding/binary, io | Libraries for decoding |
| MP3, OGG, FLAC, playback | Not included | Third-party modules or ffmpeg |
Import the format packages for their side effects so image.Decode recognises them.
This converts a JPEG into a PNG. Use image.DecodeConfig first when you only need the dimensions, because it avoids decoding the whole file. Limit the accepted size of uploads, since a small compressed file can describe a huge image.
Every image.Image exposes At(x, y) and Bounds(). Convert to grayscale with the colour model:
Direct access through the concrete types (GrayAt, RGBAAt, or the Pix slice) is much faster than calling At on the interface, which allocates a colour value each time.
The standard library cannot resize, but cropping works through SubImage. For scaling, use the golang.org/x/image/draw module.
Keep the aspect ratio in real code by calculating the height from the width. Process large batches with a worker pool, using a buffered channel and a fixed number of goroutines.
A WAV file is a 44-byte header followed by raw samples, so you can write one with encoding/binary. This program generates one second of a 440 Hz sine tone.
For decoding WAV, MP3 or FLAC and for playback, use a maintained module, or call ffmpeg through os/exec and read its output. Using ffmpeg is often the most robust choice for conversion pipelines.
io.Reader and io.Writer, and wrap files in bufio to cut system calls.sync.Pool in hot paths.Pix slices for pixel loops.errgroup from golang.org/x/sync.image.DecodeConfig dimensions before decoding untrusted uploads, to prevent memory exhaustion.go test -bench and profile with pprof before optimising.Not directly. Use golang.org/x/image/draw, which is maintained by the Go team, or a dedicated library.
Decoding is available in golang.org/x/image/webp. Encoding needs a third-party module or an external tool.
The standard library cannot play sound. Use a module that wraps the operating system's audio API.
Call image.DecodeConfig first, reject dimensions above your limit, and process images one at a time or in a small worker pool.
The standard library handles image decoding, encoding, colour and compositing well, while resizing, text and most audio formats come from golang.org/x modules or tools such as ffmpeg. Use concrete image types for speed, bound memory on untrusted input, and parallelise with a worker pool.