Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

gopherjs

> 编程语言
Open source

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

13.2K stars0 likes2 views
WebsiteGitHub

About

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

GopherJS - A compiler from Go to JavaScript

GopherJS compiles Go code (go.dev) 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.

Help us make GopherJS better!

  • ⤴️ Help us make better decisions by filling a quick 15-question GopherJS user survey.
  • Report and discuss issues.
  • Share your knowledge and experience through articles and documentation.
  • ️ Write GopherJS bindings for other libraries or contribute to GopherJS itself.

What's new?

  • 2026-06-26: Go 1.21 support is released!
  • 2026-01-02: Go 1.20 support is released!
  • 2025-08-19: Go 1.19 beta2 is released, with full generics support!
  • 2024-02-24: Go 1.19 support is available!
  • 2022-08-18: Go 1.18 support is available!
  • 2021-09-19: Go 1.17 support is available!
  • 2021-08-23: Go Modules are now fully supported.
  • 2021-06-19: Complete syscall/js package implementation compatible with the upstream Go 1.16.
  • 2021-04-04: Go 1.16 is now officially supported!

Playground

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.21 or newer. If you need an older Go version, you can use an older GopherJS release.

Install GopherJS with go install:

bash
go install github.com/gopherjs/[email protected]  # Or replace 'v1.21.0' with another version.

If your local Go distribution as reported by go version is newer than Go 1.21, then you need to set the GOPHERJS_GOROOT environment variable to a directory that contains a Go 1.21 distribution. For example:

bash
go install golang.org/dl/go1.21.13@latest
go1.21.13 download
export GOPHERJS_GOROOT="$(go1.21.13 env GOROOT)"  # Also add this line to your .profile or equivalent.

Now you can use gopherjs build [package], gopherjs build [files] or gopherjs install [package] which behave similar to the go tool. For main packages, these commands create a .js file and .js.map source map in the current directory or in $GOPATH/bin. 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.

gopherjs uses your platform's default GOOS value when generating code. Supported GOOS values are: linux, darwin. If you're on a different platform (e.g., Windows or FreeBSD), you'll need to set the GOOS 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.

Mac LC_UUID Error

On Macs, if you compile the gopherjs app with anything less than go1.24, you may get the following error:

plain
dyld[60166]: missing LC_UUID load command in ...

This is a Go + Mac issued fixed in go1.24. This is only an issue because gopherjs hasn't reached go1.24 support yet.

If you get this error, build the gopherjs app with go1.24+. However, because of how we target specific STL versions, you must still use the correct STL for the version of gopherjs that you have. See how to set GOPHERJS_GOROOT above for how to select the correct STL that gopherjs will use.

gopherjs run, gopherjs test

If you want to use gopherjs run or gopherjs test to run the generated code locally, install Node.js 18 (or newer).

On supported GOOS platforms, it's possible to make system calls (file system access, etc.) available. See doc/syscalls.md 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 http://localhost:8080/example.com/user/project/ should compile and run the Go package example.com/user/project. The generated JavaScript output will be served at http://localhost:8080/example.com/user/project/project.js (the .js file name will be equal to the base directory name). If the directory contains index.html it will be served, otherwise a minimal index.html that includes <script src="project.js"></script> 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 github.com/user/project then the generated JavaScript for the package github.com/user/project/mypkg will be served at http://localhost:8080/mypkg/mypkg.js.

Environment Variables

There are some GopherJS-specific environment variables:

  • GOPHERJS_GOROOT - if set, GopherJS uses this value as the default GOROOT value, instead of using the system GOROOT as the default GOROOT value
  • GOPHERJS_SKIP_VERSION_CHECK - 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 -m command line flag to generate minified code.
  • Apply gzip compression (https://en.wikipedia.org/wiki/HTTP_compression).
  • Use int instead of (u)int8/16/32/64.
  • Use float64 instead of float32.

Community

  • #gopherjs Channel on Gophers Slack (invites to Gophers Slack are available here)
  • Bindings to JavaScript APIs and libraries
  • GopherJS Blog
  • GopherJS on Twitter
  • Examples, tutorials and blogs

Getting started

Interacting with the DOM

The package github.com/gopherjs/gopherjs/js (see documentation) provides functions for interacting with native JavaScript APIs. For example the line

javascript
document.write("Hello world!");

would look like this in Go:

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:

go
package main

import "github.com/gopherjs/gopherjs/js"

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

type Pet struct {
	name string
}

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

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

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

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

Architecture

General

GopherJS emulates a 32-bit environment. This means that int, uint and uintptr have a precision of 32 bits. However, the explicit 64-bit integer types int64 and uint64 are supported.

The GOOS value of this environment is js, and the GOARCH value is ecmascript. You may use these values in build constraints when writing platform-specific code. (GopherJS 1.17 and older used js as the GOARCH value.)

Application Lifecycle

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

In the browser, calling os.Exit (e.g. indirectly by log.Fatal) also does not terminate the execution of the program. For convenience, it calls runtime.Goexit to immediately terminate the calling goroutine.

Goroutines

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:

go
js.Global.Get("myButton").Call("addEventListener", "click", func() {
  go func() {
    [...]
    someBlockingFunction()
    [...]
  }()
})

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.

Issues· 190 open

View all issuesOpen on GitHub
  • #1334

    Add Go 1.22 Support

    Updated Jul 23, 2026
  • #698

    Tight loop in goroutine never yields

    Updated May 11, 2026
  • #843

    Typecasting nil results in non-nil

    bugNeedsFixNeedsHelpUpdated Apr 26, 2026
  • #1158

    GopherJS Playground 2.0

    enhancementNeedsHelpUpdated Apr 23, 2026
  • #136

    Large file sizes

    enhancementLongTermUpdated Jan 6, 2026
  • #1382

    proposal: Add `gopherjs list` subcommand

    Proposal AcceptedUpdated Aug 18, 2025
  • #652

    Bug in overflow multiplication of uint type.

    bugNeedsFixUpdated Aug 7, 2025
  • #524

    Proposal: add option to build browserify / es6 modules

    ProposalUpdated Apr 20, 2025
  • #1341

    Possible overlooked mistakes in prelude/types.js?

    NeedsInvestigationUpdated Nov 17, 2024
  • #1316

    Support registering to globalThis

    enhancementNeedsHelpUpdated Oct 25, 2024

> Tags

Gocompilergogolanghacktoberfest

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言