# Gzip

Middleware gzip provides compress to responses for Macaron [Instances](/core_concepts.md#instances). Make sure to register it before other middlewares that write content to response.

* [GitHub](https://github.com/go-macaron/gzip)
* [API Reference](https://gowalker.org/github.com/go-macaron/gzip)

## Installation

```bash
go get github.com/go-macaron/gzip
```

## Usage

```go
package main

import (
    "github.com/go-macaron/gzip"
    "gopkg.in/macaron.v1"
)

func main() {
    m := macaron.Classic()
    m.Use(gzip.Gziper())
    // Register routers.
    m.Run()
}
```

In this case, the static files will not be compressed by Gzip, to compress them:

```go
package main

import (
    "github.com/go-macaron/gzip"
    "gopkg.in/macaron.v1"
)

func main() {
    m := macaron.New()
    m.Use(macaron.Logger())
    m.Use(macaron.Recovery())
    m.Use(gzip.Gziper())
    m.Use(macaron.Static("public"))
    // Register routers.
    m.Run()
}
```

Or you can choose to only compress a group of routes' responses:

```go
// ...

func main() {
    m := macaron.Classic()
    m.Group("/gzip", func() {
        // ...
    }, gzip.Gziper())
    // ...
    m.Run()
}
```

## Options

This service comes with a variety of configuration options([`gzip.Options`](https://gowalker.org/github.com/go-macaron/gzip#Options)):

```go
// ...
m.Use(gzip.Gziper(gzip.Options{
    // Compression level. Can be DefaultCompression(-1), ConstantCompression(-2)
    // or any integer value between BestSpeed(1) and BestCompression(9) inclusive.
    // Default is 4.
    CompressionLevel: 4,
}))
// ...
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://go-macaron.com/middlewares/gzip.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
