minformat

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 7 Imported by: 1

README

Go Report Card GoDoc

go/minformat

This package formats the Go source code in a way so it becomes more compact. It can be considered to be a minifier, although it doesn't make irreversible transformations by default (well, it does remove all comments).

The result can become readable again after running go/format, making this pipeline possible:

  1. Minify the code before transferring it over a network
  2. Send the (potentially further compressed) minified source code
  3. On the receiving side, run gofmt to get the canonical formatting

For (3) I would recommend using gofumpt.

Example

Suppose that we have this hello.go file:

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground")
}

It will be formatted into this:

package main;import("fmt");func main(){fmt.Println("Hello, playground")}

Depending on the file, it usually cuts 10-50% of the file size.

Documentation

Overview

Package minformat implements Go source code minification routines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Node

func Node(w io.Writer, fset *token.FileSet, node interface{}) error

Node formats node by removing as much whitespace as possible and writes the result to w.

Result contains no comments.

The node type is defined as interface{} for compatibility with go/format.Node function. Only ast.Node types are supported right now.

The function may return early (before the entire result is written) and return a formatting error, for instance due to an incorrect AST.

func Source

func Source(src []byte) ([]byte, error)

Source formats src by removing as mych whitespace as possible and returns the result.

src is expected to be a syntactically correct Go source file.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL