github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/README.md (about)

     1  ## GopherJS - A compiler from Go to JavaScript
     2  
     3  [![GoDoc](https://godoc.org/github.com/gopherjs/gopherjs/js?status.svg)](https://godoc.org/github.com/gopherjs/gopherjs/js)
     4  [![Sourcegraph](https://sourcegraph.com/github.com/gopherjs/gopherjs/-/badge.svg)](https://sourcegraph.com/github.com/gopherjs/gopherjs?badge)
     5  [![Circle CI](https://circleci.com/gh/gopherjs/gopherjs.svg?style=svg)](https://circleci.com/gh/gopherjs/gopherjs)
     6  
     7  GopherJS compiles Go code ([go.dev](https://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.
     8  
     9  ### Help us make GopherJS better!
    10  
    11  - โคด๏ธ **Help us make better decisions by filling a quick 15-question [GopherJS user survey](https://forms.gle/WEjZqZaPxTxjD9YP8)**.
    12  - ๐Ÿ“ข Report and discuss [issues](https://github.com/gopherjs/gopherjs/issues).
    13  - ๐ŸŽ“ Share your knowledge and experience through [articles](https://github.com/gopherjs/gopherjs/wiki/Community-Tutorials-and-Blogs) and [documentation](https://github.com/gopherjs/gopherjs/tree/master/doc).
    14  - ๐Ÿ› ๏ธ Write GopherJS [bindings](https://github.com/gopherjs/gopherjs/wiki/Bindings) for other libraries or [contribute](https://github.com/gopherjs/gopherjs/wiki/Developer-Guidelines) to GopherJS itself.
    15  
    16  ### What's new?
    17  
    18  - 2024-02-24: Go 1.19 support is [available](https://github.com/gopherjs/gopherjs/releases/tag/v1.19.0-beta1)!
    19  - 2022-08-18: Go 1.18 support is [available](https://github.com/gopherjs/gopherjs/releases/tag/v1.18.0-beta2%2Bgo1.18.5)!
    20  - 2021-09-19: Go 1.17 support is available!
    21  - 2021-08-23: Go Modules are now fully supported.
    22  - 2021-06-19: Complete `syscall/js` package implementation compatible with the upstream Go 1.16.
    23  - 2021-04-04: **Go 1.16 is now officially supported!** ๐ŸŽ‰ ๐ŸŽ‰ ๐ŸŽ‰
    24  
    25  ### Playground
    26  
    27  Give GopherJS a try on the [GopherJS Playground](http://gopherjs.github.io/playground/).
    28  
    29  ### What is supported?
    30  
    31  Nearly everything, including Goroutines ([compatibility documentation](https://github.com/gopherjs/gopherjs/blob/master/doc/compatibility.md)). Performance is quite good in most cases, see [HTML5 game engine benchmark](https://ajhager.github.io/engi/demos/botmark.html). Cgo is not supported.
    32  
    33  ### Installation and Usage
    34  
    35  GopherJS [requires Go 1.19 or newer](https://github.com/gopherjs/gopherjs/blob/master/doc/compatibility.md#go-version-compatibility). If you need an older Go
    36  version, you can use an [older GopherJS release](https://github.com/gopherjs/gopherjs/releases).
    37  
    38  Install GopherJS with `go install`:
    39  
    40  ```
    41  go install github.com/gopherjs/gopherjs@v1.19.0-beta1  # Or replace 'v1.19.0-beta1' with another version.
    42  ```
    43  
    44  If your local Go distribution as reported by `go version` is newer than Go 1.19, then you need to set the `GOPHERJS_GOROOT` environment variable to a directory that contains a Go 1.19 distribution. For example:
    45  
    46  ```
    47  go install golang.org/dl/go1.19.13@latest
    48  go1.19.13 download
    49  export GOPHERJS_GOROOT="$(go1.19.13 env GOROOT)"  # Also add this line to your .profile or equivalent.
    50  ```
    51  
    52  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.
    53  
    54  `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]`.
    55  
    56  _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._
    57  
    58  #### gopherjs run, gopherjs test
    59  
    60  If you want to use `gopherjs run` or `gopherjs test` to run the generated code locally, install Node.js 10.0.0 (or newer), and the `source-map-support` module:
    61  
    62  ```
    63  npm install --global source-map-support
    64  ```
    65  
    66  On supported `GOOS` platforms, it's possible to make system calls (file system access, etc.) available. See [doc/syscalls.md](https://github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md) for instructions on how to do so.
    67  
    68  #### gopherjs serve
    69  
    70  `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.
    71  
    72  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.
    73  
    74  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.
    75  
    76  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.
    77  
    78  #### Environment Variables
    79  
    80  There are some GopherJS-specific environment variables:
    81  
    82  - `GOPHERJS_GOROOT` - if set, GopherJS uses this value as the default GOROOT
    83    value, instead of using the system GOROOT as the default GOROOT value
    84  - `GOPHERJS_SKIP_VERSION_CHECK` - if set to true, GopherJS will not check
    85    Go version in the GOROOT for compatibility with the GopherJS release. This
    86    is primarily useful for testing GopherJS against unreleased versions of Go.
    87  
    88  ### Performance Tips
    89  
    90  - Use the `-m` command line flag to generate minified code.
    91  - Apply gzip compression (https://en.wikipedia.org/wiki/HTTP_compression).
    92  - Use `int` instead of `(u)int8/16/32/64`.
    93  - Use `float64` instead of `float32`.
    94  
    95  ### Community
    96  
    97  - [#gopherjs Channel on Gophers Slack](https://gophers.slack.com/messages/gopherjs/) (invites to Gophers Slack are available [here](http://blog.gopheracademy.com/gophers-slack-community/#how-can-i-be-invited-to-join:2facdc921b2310f18cb851c36fa92369))
    98  - [Bindings to JavaScript APIs and libraries](https://github.com/gopherjs/gopherjs/wiki/bindings)
    99  - [GopherJS Blog](https://medium.com/gopherjs)
   100  - [GopherJS on Twitter](https://twitter.com/GopherJS)
   101  - [Examples, tutorials and blogs](https://github.com/gopherjs/gopherjs/wiki/Community-Tutorials-and-Blogs)
   102  
   103  ### Getting started
   104  
   105  #### Interacting with the DOM
   106  
   107  The package `github.com/gopherjs/gopherjs/js` (see [documentation](https://godoc.org/github.com/gopherjs/gopherjs/js)) provides functions for interacting with native JavaScript APIs. For example the line
   108  
   109  ```js
   110  document.write("Hello world!");
   111  ```
   112  
   113  would look like this in Go:
   114  
   115  ```go
   116  js.Global.Get("document").Call("write", "Hello world!")
   117  ```
   118  
   119  You may also want use the [DOM bindings](http://dominik.honnef.co/go/js/dom), the [jQuery bindings](https://github.com/gopherjs/jquery) (see [TodoMVC Example](https://github.com/gopherjs/todomvc)) or the [AngularJS bindings](https://github.com/wvell/go-angularjs). Those are some of the [bindings to JavaScript APIs and libraries](https://github.com/gopherjs/gopherjs/wiki/bindings) by community members.
   120  
   121  #### Providing library functions for use in other JavaScript code
   122  
   123  Set a global variable to a map that contains the functions:
   124  
   125  ```go
   126  package main
   127  
   128  import "github.com/gopherjs/gopherjs/js"
   129  
   130  func main() {
   131  	js.Global.Set("pet", map[string]interface{}{
   132  		"New": New,
   133  	})
   134  }
   135  
   136  type Pet struct {
   137  	name string
   138  }
   139  
   140  func New(name string) *js.Object {
   141  	return js.MakeWrapper(&Pet{name})
   142  }
   143  
   144  func (p *Pet) Name() string {
   145  	return p.name
   146  }
   147  
   148  func (p *Pet) SetName(name string) {
   149  	p.name = name
   150  }
   151  ```
   152  
   153  For more details see [Jason Stone's blog post](http://legacytotheedge.blogspot.de/2014/03/gopherjs-go-to-javascript-transpiler.html) about GopherJS.
   154  
   155  ### Architecture
   156  
   157  #### General
   158  
   159  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.
   160  
   161  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](doc/compatibility.md#how-to-write-portable-code). (GopherJS 1.17 and older used `js` as the `GOARCH` value.)
   162  
   163  #### Application Lifecycle
   164  
   165  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.
   166  
   167  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.
   168  
   169  #### Goroutines
   170  
   171  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:
   172  
   173  ```go
   174  js.Global.Get("myButton").Call("addEventListener", "click", func() {
   175    go func() {
   176      [...]
   177      someBlockingFunction()
   178      [...]
   179    }()
   180  })
   181  ```
   182  
   183  How it works:
   184  
   185  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.
   186  
   187  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.
   188  
   189  ### GopherJS Development
   190  
   191  If you're looking to make changes to the GopherJS compiler, see [Developer Guidelines](https://github.com/gopherjs/gopherjs/wiki/Developer-Guidelines) for additional developer information.