Skip to content
Static file handler for Gin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Static file serving for Gin. Compress assets such as .js, .css using gzip. All static assets are also served with proper cache control headers.


Download and install:

go get

Import on your code and start using it:

import ""

If pngcrush is available it will be used to crush .png images


package main

import ""

func main() {
	// Load and compile static assets
	// You can now access static.AssetMap that contains
	// All compiled static files

	// Create gin router
	router := gin.Default()

	// Set asset func
		"asset": static.Asset,

	// Declare our static path
	router.GET("/static/*filepath", static.ServeStaticFiles)

On your templates you can access the compiled assets using the asset function:

		<h2>Hello World!</h2>
	<script src="{{ asset "/public/js/test.js" }}"></script>


gin-static will load all your static files, minify them (.css and .js files) and gzip them. You can later access the files using the variable AssetMap (contains all loaded static files).

You can also access the static files on your .html templates, first you need to declare the provided Asset function on your gin router func map.

While running on development mode assets you should add a static handler route, so you dont need to recompile the static folder. The {{ asset }} function will check if gin is on debug mode, and return a path using /public so you should declare the following router to be able to work on development mode:

if gin.Mode() == gin.DebugMode {
	r.Static("/public", "public")

If you ever make changes to your static files, you will need to restart your application, you could use this as a separate server just to serve static files


gin-static is licensed under the MIT license.

You can’t perform that action at this time.