GopherJS includes a sub-package called js. This package provides the functionalities needed to bridge between Go and JavaScript. The package can be found at https://godoc.org/github.com/gopherjs/gopherjs/js.
The key feature provided by the js package is the ability to transform Go types into JavaScript types, and vice versa.
When we consider data types, there are two main categories that need to be supported: basic types (int, float, and string) and constructed types (structs and interfaces). The following table shows the type mappings between Go basic types and JavaScript types, as supported by GopherJS:
Go type | JavaScript type |
bool | Boolean |
int and float | Number |
string | String |
[]int8 | Int8Array |
[]int16 | Int16Array |
[]int32, []int | Int32Array |
[]uint8 | Uint8Array |
[]uint16 | Uint16Array |
[]uint32, []uint | Uint32Array |
[]float32 | Float32Array |
[]float64 |
Float64Array |
For example, if you utilize GopherJS to compile a piece of code that includes a Go int type into JavaScript, the int type will become a JavaScript Number type. It is recommended that you stick with the int type, instead of the uint8/uint16/uint32/uint64 types, to improve performance of your transpiled code. It is also recommended to use float64 over float32.
Now that we know the different GopherJS types, let's move on to object types.