Created
December 22, 2021 09:12
-
-
Save elsanussi-s-mneina/340bbcfdf14669258623f0af9fa2f5c9 to your computer and use it in GitHub Desktop.
some notes to save lessons learned while setting up gopherjs to transpile go to javascript on MacOS BigSur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notes on Gopherjs installation on MacOS BigSur: | |
First you must know the version number to know if these notes are relevant to you: | |
% sw_vers | |
ProductName: macOS | |
ProductVersion: 11.6.1 | |
BuildVersion: 20G224 | |
% go version | |
go version go1.17.5 darwin/amd64 | |
% ~/go/bin/gopherjs version | |
GopherJS 1.17.1+go1.17.3 | |
1. You must have already added ~/go/bin to your PATH environment variable. This is where Go installs executables when you run the "go install" command. | |
2. run: | |
go install github.com/gopherjs/gopherjs@latest | |
Don't do the instructions they suggest. go get is deprecated the way it was indicated in the ReadMe file. | |
3. Run the following three commands. These are indicated in the readme, but they still use the deprecated go get instead of go install. I have fixed that in the following three lines. | |
go install golang.org/dl/go1.17.1@latest | |
go1.17.1 download | |
export GOPHERJS_GOROOT="$(~/go/bin/go1.17.1 env GOROOT)" | |
5. This is how you would generate an exampleAlert.js (javascript) file from a Go program. | |
gopherjs build -m exampleAlert.go | |
Note here are the contents of the exampleAlert.go program (after the cat command) | |
% cat exampleAlert.go | |
package main | |
import ( | |
"syscall/js" | |
) | |
func main() { | |
//fmt.Println("Hello, playground") | |
js.Global().Call("alert", "Hello, JavaScript") | |
println("Hello, JS console") | |
} | |
5. Create an HTML file for example "index.html", be sure to import the javascript file. | |
% cat index.html | |
<html> | |
<head> | |
<title>Hello alert example</title> | |
</head> | |
<body> | |
<script src="exampleAlert.js"></script> | |
</body> | |
</html> | |
4. On MacOS BigSur, do not use the fmt package because you will get a runtime error. According to the Issue on Github for gopherJS you should set the GOOS variable to linux, but I have not verified this working. They will fix this in the next version of GopherJS. | |
Notes written on December 22, 2021 | |
This work (this text file) is licensed under a Creative Commons Attribution 4.0 International License. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment