This is a Prettier v3 plugin for the Go programming language. It formats Go code using the official go/format
package, which is compiled to WebAssembly (WASM) to run in a Node.js environment.
As web developers, we love the consistent code style that Prettier brings to our projects. However, when working on projects that involve Go - for instance, a Go backend with a JavaScript frontend, or writing Go code in a web-based editor - the ability to format Go code with the same familiar tool was missing.
This project was born out of the desire to have a single, unified formatting toolchain. Instead of relying on external gofmt
binaries, which can be brittle to manage in a Node.js-based development environment, this plugin leverages the power of WebAssembly. We compile Go's own formatting tools into a WASM module, allowing you to format Go code directly within your JavaScript-based tools, ensuring speed, portability, and consistency.
First, install Prettier (v3 or later) and this plugin from NPM:
npm install --save-dev prettier go-prettier-format
The plugin comes with a pre-compiled WASM module, so you don't need to have Go installed on your machine to use it.
You can run Prettier from the command line to format your Go files:
# Format a specific file
npx prettier --write your-file.go
# Format all .go files in the project
npx prettier --write "**/*.go"
Note: For cross-platform compatibility in npm scripts, it's recommended to use single quotes:
'**/*.go'
.
For convenience, you can add a script to your package.json
:
{
"scripts": {
"format": "prettier --write '**/*.go'"
}
}
Once installed, Prettier-compatible editors (like VS Code with the Prettier extension) should automatically pick up the plugin and use it to format .go
files on save.
Interested in contributing? Here's how to get set up for development.
You will need to have Go (1.21+) and Node.js (18+) installed on your machine.
-
Clone the repository and install dependencies:
git clone https://github.com/gitdog01/go-prettier-format.git cd go-prettier-format npm install
-
Build the WASM module:
npm run build:wasm
If you prefer not to install Go and Node.js locally, you can use Docker:
-
Clone the repository:
git clone https://github.com/gitdog01/go-prettier-format.git cd go-prettier-format
-
Build using Docker:
# Build the project docker build -t go-prettier-format . # Run tests docker run --rm go-prettier-format # Extract built artifacts (optional) docker run --rm -v $(pwd):/output go-prettier-format sh -c "cp go.wasm /output/ && cp -r dist /output/"
-
Development with Docker Compose (recommended):
# Build the project docker-compose run --rm build # Run tests docker-compose run --rm test # Development mode (build + test) docker-compose run --rm dev
If you just want to try the plugin without installing Go or Node.js:
# Clone and test
git clone https://github.com/gitdog01/go-prettier-format.git
cd go-prettier-format
docker-compose run --rm test
This repository comes with a set of test files to verify the plugin's functionality.
To run the tests:
npm test
This command will:
- Build the latest version of the plugin from source.
- Create a
test_result
directory. - Copy the sample files from the
tests/
directory into it. - Run the Prettier formatter on the files inside
test_result/
.
You can then compare the original files in tests/
with the formatted files in test_result/
to see the plugin in action. For example, tests/messy.txt
will be cleaned up, while tests/error.txt
(which contains a syntax error) will remain untouched.
Note: The test files use a
.txt
extension to prevent editors from automatically formatting them. The test script tells Prettier to treat these.txt
files as Go code.
This package is configured to build automatically before publishing.
- Update the version number in
package.json
usingnpm version <patch|minor|major>
. - Run
npm publish
.
The prepublishOnly
script will handle building the Wasm and JavaScript bundles before the package is uploaded to the registry.
- The core formatting logic from Go's standard library (
go/format
) is compiled into a WebAssembly (.wasm
) file. - The
wasm_exec.js
script provided by Go is used to load and run the WASM module in Node.js. - This Prettier plugin loads the WASM module and exposes the Go formatting function to Prettier's printing process.
- When Prettier processes a
.go
file, this plugin passes the code to the WASM module, which formats it and returns the result.
This approach ensures that the formatting is identical to what gofmt
would produce, without needing a Go installation on the machine running the formatter.