You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: index.md
+56-41Lines changed: 56 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -4,81 +4,96 @@ title: Home
4
4
permalink: /
5
5
---
6
6
7
-
Go -> JavaScript
8
-
----------------
7
+
GopherJS - A compiler from Go to JavaScript
8
+
-------------------------------------------
9
9
10
10
GopherJS compiles Go code ([golang.org](http://golang.org/)) 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. Give GopherJS a try on the [GopherJS Playground](http://gopherjs.github.io/playground/).
11
11
12
-
You can take advantage of Go's elegant type system and other compile-time checks that can have a huge impact on bug detection and the ability to refactor, especially for big projects. Just think of how often a JavaScript method has extra handling of some legacy parameter scheme, because you don't know exactly if some other code is still calling it in that old way or not. GopherJS will tell you and if it does not complain, you can be sure that this kind of bug is not present any more.
13
-
14
-
### Design Goals
15
-
- performance of generated code (see [HTML5 game engine benchmark](http://ajhager.github.io/enj/) by Joseph Hager)
16
-
- similarity between Go code and generated JavaScript code for easier debugging
17
-
- compatibility with existing libraries (see the list of [bindings to JavaScript APIs and libraries](https://github.com/gopherjs/gopherjs/wiki/bindings))
18
-
- small size of generated code
19
-
20
12
### What is supported?
21
-
Nearly everything, including Goroutines. See the [compatibility table](//github.com/gopherjs/gopherjs/blob/master/doc/packages.md) for details.
22
-
23
-
### Goroutines
24
-
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.
25
-
26
-
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. This is done by preserving each stack frame inside a closure.
27
-
28
-
The performance of the code generated by this approach is quite good, but not as good as the simple nonblocking version. That's why GopherJS tries to be conservative about it. It will scan your code for functions that use blocking instructions and mark them as blocking accordingly. Then it will recursively mark all functions as blocking which have a call to some other function which is already know to be blocking. This works well for calls to package functions and for method calls on non-interface types. For calls to interface methods and to `func` values, however, it is not exactly known at compile time which function will be executed at runtime. In those cases you need to mark the call with the comment `//gopherjs:blocking` (no space after the slashes). Else, the call will panic at runtime.
29
-
30
-
Also, callbacks from external JavaScript code into Go code (see below) can never be blocking, but you may use the `go` statement to create a new goroutine that may have blocking behavior.
13
+
Nearly everything, including Goroutines ([compatibility table](doc/packages.md)). Performance is quite good in most cases, see [HTML5 game engine benchmark](http://ajhager.github.io/enj/).
31
14
32
15
### Installation and Usage
33
16
Get or update GopherJS and dependencies with:
34
-
35
-
```bash
17
+
```
36
18
go get -u github.com/gopherjs/gopherjs
37
19
```
38
-
39
-
Now you can use `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. If you want to run the generated code with Node.js, see [this page](//github.com/gopherjs/gopherjs/blob/master/doc/syscalls.md).
20
+
Now you can use `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. If you want to run the generated code with Node.js, see [this page](doc/syscalls.md).
40
21
41
22
*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.*
42
23
43
-
### Getting started
24
+
### Performance Tips
44
25
45
-
#### 1. Interacting with the DOM
46
-
The package `github.com/gopherjs/gopherjs/js` (see [documentation](http://godoc.org/github.com/gopherjs/gopherjs/js)) provides functions for interacting with native JavaScript APIs. For example the line
26
+
- Use the `-m` command line flag to generate minified code.
-[Bindings to JavaScript APIs and libraries](https://github.com/gopherjs/gopherjs/wiki/bindings)
34
+
-[GopherJS on Twitter](https://twitter.com/GopherJS)
47
35
36
+
### Getting started
37
+
#### Interacting with the DOM
38
+
The package `github.com/gopherjs/gopherjs/js` (see [documentation](http://godoc.org/github.com/gopherjs/gopherjs/js)) provides functions for interacting with native JavaScript APIs. For example the line
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/gopherjs/go-angularjs). Those are some of the [bindings to JavaScript APIs and libraries](https://github.com/gopherjs/gopherjs/wiki/bindings) by community members.
59
47
60
-
#### 2. Providing library functions for use in other JavaScript code
48
+
#### Providing library functions for use in other JavaScript code
61
49
Set a global variable to a map that contains the functions:
For more details see [Jason Stone's blog post](http://legacytotheedge.blogspot.de/2014/03/gopherjs-go-to-javascript-transpiler.html) about GopherJS.
80
78
81
-
### Community
82
-
- Get help in the [Google Group](https://groups.google.com/d/forum/gopherjs)
83
-
- See the list of [bindings to JavaScript APIs and libraries](https://github.com/gopherjs/gopherjs/wiki/bindings) by community members
84
-
- Follow [GopherJS on Twitter](https://twitter.com/GopherJS)
79
+
### Architecture
80
+
81
+
#### General
82
+
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 `GOARCH` value of GopherJS is "js". You may use it as a build constraint: `// +build js`.
83
+
84
+
#### Goroutines
85
+
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:
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.
98
+
99
+
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. This is done by preserving each stack frame inside a closure.
0 commit comments