Skip to content

GCSS: compiling indented CSS from Go

GCSS is a CSS preprocessor written in pure Go. You write an indented, brace-free syntax and it outputs CSS. Because it is a Go package, you can compile stylesheets inside a Go web server or build tool without installing Node or Ruby.

The library, inside a Go module:

Terminal window
go get github.com/yosssi/gcss

The command-line compiler:

Terminal window
go install github.com/yosssi/gcss/cmd/gcss@latest

GCSS supports four features: variables, mixins, nested selectors, and & for the parent selector. Lines starting with // are comments and are dropped from the output. There are no loops, conditionals or arithmetic.

Indentation must be exactly two spaces per level; GCSS counts spaces and divides by two. Properties are written name: value with a space after the colon and no trailing semicolon.

nav
ul
margin: 0
padding: 0
a
color: blue
&:hover
color: red

compiles to (GCSS emits it on one line; formatted here for reading):

nav ul{margin:0;padding:0;}
a{color:blue;}
a:hover{color:red;}

Variables are declared at the top level with a $ prefix:

$base-font: Helvetica, sans-serif
$main-color: #0074d9
body
font-family: $base-font
color: $main-color

A mixin is declared at the top level like a variable with parameters, and invoked inside a selector with the same $name(...) form:

$border-radius($radius)
-webkit-border-radius: $radius
border-radius: $radius
.box
$border-radius(10px)

Note that this is not Sass syntax: there is no @mixin or @include. Other at-rules such as @media or @import are passed through to the output.

Declare variables and mixins before the selectors that use them. GCSS compiles the file in a single pass, so a variable used before its declaration comes out as an empty value rather than an error.

gcss.Compile reads GCSS from an io.Reader and writes CSS to an io.Writer. It returns the number of bytes written and an error:

package main
import (
"log"
"os"
"github.com/yosssi/gcss"
)
func main() {
src, err := os.Open("style.gcss")
if err != nil {
log.Fatal(err)
}
defer src.Close()
dst, err := os.Create("style.css")
if err != nil {
log.Fatal(err)
}
defer dst.Close()
if _, err := gcss.Compile(dst, src); err != nil {
log.Fatalf("compiling GCSS: %v", err)
}
}

If you just want a .css file next to the source, gcss.CompileFile does the reading and writing for you and returns the output path:

cssPath, err := gcss.CompileFile("style.gcss") // writes style.css
if err != nil {
log.Fatal(err)
}
log.Println("wrote", cssPath)
Terminal window
# writes style.css next to style.gcss
gcss style.gcss
# or read stdin and write stdout
gcss < style.gcss > style.css

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)