Need help with gopherjs?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

10.5K Stars 489 Forks BSD 2-Clause "Simplified" License 2.3K Commits 217 Opened issues


A compiler from Go to JavaScript for running Go code in a browser

Services available


Need anything else?

Contributors list

GopherJS - A compiler from Go to JavaScript

GoDoc Sourcegraph Circle CI

GopherJS compiles Go code ( to pure JavaScript code. Its main purpose is to give you the opportunity to write front-end code in Go which will still run in all browsers.

What's new?

  • 2021-09-19: Go 1.17 support is available!
  • 2021-08-23: Go Modules are now fully supported.
  • 2021-06-19: Complete
    package implementation compatible with the upstream Go 1.16.
  • 2021-04-04: Go 1.16 is now officially supported! 🎉 🎉 🎉


Give GopherJS a try on the GopherJS Playground.

What is supported?

Nearly everything, including Goroutines (compatibility documentation). Performance is quite good in most cases, see HTML5 game engine benchmark. Cgo is not supported.

Installation and Usage

GopherJS requires Go 1.17 or newer. If you need an older Go version, you can use an older Gopher release.

Get or update GopherJS and dependencies with:

go get -u

If your local Go distribution as reported by

go version
is newer than Go 1.17, then you need to set the
environment variable to a directory that contains a Go 1.17 distribution. For example:
go get
go1.17.1 download
export GOPHERJS_GOROOT="$(go1.17.1 env GOROOT)"  # Also add this line to your .profile or equivalent.

Now you can use

gopherjs build [package]
gopherjs build [files]
gopherjs install [package]
which behave similar to the
tool. For
packages, these commands create a
file and
source map in the current directory or in
. The generated JavaScript file can be used as usual in a website. Use
gopherjs help [command]
to get a list of possible command line flags, e.g. for minification and automatically watching for changes.

uses your platform's default
value when generating code. Supported
values are:
. If you're on a different platform (e.g., Windows or FreeBSD), you'll need to set the
environment variable to a supported value. For example,
GOOS=linux gopherjs build [package]

Note: GopherJS will try to write compiled object files of the core packages to your $GOROOT/pkg directory. If that fails, it will fall back to $GOPATH/pkg.

gopherjs run, gopherjs test

If you want to use

gopherjs run
gopherjs test
to run the generated code locally, install Node.js 10.0.0 (or newer), and the
npm install --global source-map-support

On supported

platforms, it's possible to make system calls (file system access, etc.) available. See doc/ for instructions on how to do so.

gopherjs serve

gopherjs serve
is a useful command you can use during development. It will start an HTTP server serving on ":8080" by default, then dynamically compile your Go packages with GopherJS and serve them.

For example, navigating to

should compile and run the Go package
. The generated JavaScript output will be served at
(the .js file name will be equal to the base directory name). If the directory contains
it will be served, otherwise a minimal
that includes
 will be provided, causing the JavaScript to be executed. All other static files will be served too.

Refreshing in the browser will rebuild the served files if needed. Compilation errors will be displayed in terminal, and in browser console. Additionally, it will serve $GOROOT and $GOPATH for sourcemaps.

If you include an argument, it will be the root from which everything is served. For example, if you run

gopherjs serve
then the generated JavaScript for the package will be served at http://localhost:8080/mypkg/mypkg.js.

Environment Variables

There is one GopherJS-specific environment variable:

    - if set, GopherJS uses this value as the default GOROOT value, instead of using the system GOROOT as the default GOROOT value
    - if set to true, GopherJS will not check Go version in the GOROOT for compatibility with the GopherJS release. This is primarily useful for testing GopherJS against unreleased versions of Go.

Performance Tips

  • Use the
    command line flag to generate minified code.
  • Apply gzip compression (
  • Use
    instead of
  • Use
    instead of


Interacting with the DOM

The package
(see documentation) provides functions for interacting with native JavaScript APIs. For example the line
document.write("Hello world!");

would look like this in Go:

js.Global.Get("document").Call("write", "Hello world!")

You may also want use the DOM bindings, the jQuery bindings (see TodoMVC Example) or the AngularJS bindings. Those are some of the bindings to JavaScript APIs and libraries by community members.

Providing library functions for use in other JavaScript code

Set a global variable to a map that contains the functions:

package main

import ""

func main() { js.Global.Set("pet", map[string]interface{}{ "New": New, }) }

type Pet struct { name string }

func New(name string) *js.Object { return js.MakeWrapper(&Pet{name}) }

func (p *Pet) Name() string { return }

func (p *Pet) SetName(name string) { = name }

For more details see Jason Stone's blog post about GopherJS.



GopherJS emulates a 32-bit environment. This means that

have a precision of 32 bits. However, the explicit 64-bit integer types
are supported. The
value of GopherJS is "js". You may use it as a build constraint:
// +build js,-wasm

Application Lifecycle


function is executed as usual after all
functions have run. JavaScript callbacks can also invoke Go functions, even after the
function has exited. Therefore the end of the
function should not be regarded as the end of the application and does not end the execution of other goroutines.

In the browser, calling

(e.g. indirectly by
) also does not terminate the execution of the program. For convenience, it calls
to immediately terminate the calling goroutine.


Goroutines are fully supported by GopherJS. The only restriction is that you need to start a new goroutine if you want to use blocking code called from external JavaScript:

js.Global.Get("myButton").Call("addEventListener", "click", func() {
  go func() {

How it works:

JavaScript has no concept of concurrency (except web workers, but those are too strictly separated to be used for goroutines). Because of that, instructions in JavaScript are never blocking. A blocking call would effectively freeze the responsiveness of your web page, so calls with callback arguments are used instead.

GopherJS does some heavy lifting to work around this restriction: Whenever an instruction is blocking (e.g. communicating with a channel that isn't ready), the whole stack will unwind (= all functions return) and the goroutine will be put to sleep. Then another goroutine which is ready to resume gets picked and its stack with all local variables will be restored.

GopherJS Development

If you're looking to make changes to the GopherJS compiler, see Developer Guidelines for additional developer information.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.