The GopherJS transpiler

We use the GopherJS transpiler to convert a Go program into a JavaScript program. Figure 3.5 depicts a Go program that not only makes use of the functionality from the Go standard library but also uses the functionality from various JavaScript APIs using the equivalent GopherJS bindings package:

Figure 3.5: A Go program that makes use of the standard library and GopherJS bindings transpiled to an equivalent JavaScript program

We use the gopherjs build command to transpile the Go program into its equivalent JavaScript representation. The produced JavaSript source code is not meant to be modified by humans. The JavaScript program has access to the underlying JavaScript runtime embedded in the web browser, along with access to common JavaScript APIs.

To get an idea of how types are converted from Go to JavaScript, take a look at the table available at https://godoc.org/github.com/gopherjs/gopherjs/js.

With regard to IGWEB, we have organized the front-end Go web application project code inside of the client folder. This allows us to neatly separate the front-end web application from the back-end web application.

Figure 3.6 depicts the client project folder containing numerous Go source files:

Figure 3.6: The client Folder Houses the Go source files that comprise the Front-End Go Web application. The GopherJS transpiler produces a JavaScript program (client.js) and a source map (client.js.map)

Upon running the GopherJS transpiler on the Go source files inside the client folder, by issuing the gopherjs build command, two output files are created. The first output file is the client.js file, which represents the equivalent JavaScript program. The second output file is the client.js.map file, which is a source map that's used for debugging purposes. This source map helps us when we are chasing down bugs using the web browser's console by providing us detailed information on produced errors.

The Appendix: Debugging Isomorphic Go, contains guidance and advice on debugging an isomorphic web application implemented in Go.

The gopherjs build command is synonymous in behavior with its go build counterpart. The client project folder can contain any number of subfolders, which may also contain Go source files. When we execute the gopherjs build command, a single JavaScript source program is created along with a source map file. This is analogous to the single static binary file that gets created when issuing a go build command.

Code that is shared between the server and the client, outside of the client folder, may be shared by specifying the proper path to the shared package in the import statement. The shared folder will contain code that is to be shared across environments, such as models and templates.

We can include the GopherJS produced JavaScript source file as an external javascript source file in our web page using the <script> tag, as shown here:

<script type="text/javascript" src="/js/client.js"></script>

Keep in mind that when we issue a gopherjs build command, we are not just creating a JavaScript equivalent of the program we are writing, but we are also bringing along any packages from the standard library or third-party packages that our program relies on. So in addition to including our front-end Go program, GophjerJS also includes any dependent packages that our program is dependent on.

Not all packages from the Go standard library work inside the web browser. You can reference the GopherJS compatibility table to view a list of supported packages from the Go standard library at https://github.com/gopherjs/gopherjs/blob/master/doc/packages.md.

The ramification of this fact is that the produced JavaScript source code's file size will grow proportionately to the amount of dependencies we introduce in our Go program. The other ramification of this fact is that it doesn't make sense to include multiple GopherJS produced JavaScript files on the same same web page as depicted in Figure 3.7, since dependent packages (such as common packages from the standard library) will be included multiple times, unnecessarily bulking up our total script payload and offering no value in return:

Figure 3.7: Do not import multiple GopherJS produced source files in a single web page

A web page should therefore, at maximum, include only one GopherJS produced source file, as depicted in Figure 3.8:

Figure 3.8: Only a single GopherJS produced source file should be included in a web page
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.154.64