Skip to content

Commit 2d1c299

Browse files
committed
feat(integrate-go-library-into-a-javaScript-webpage-with-web-assembly): init
1 parent c603e8f commit 2d1c299

File tree

13 files changed

+821
-1
lines changed

13 files changed

+821
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Table of contents
110110
* [Functional Programming](https://vivaxy.github.io/examples/functional-programming/) 函数式编程
111111
* [Etc.](https://vivaxy.github.io/examples/functional-programming/etc/) Etc
112112
* [Manipulate Functions](https://vivaxy.github.io/examples/functional-programming/manipulate-functions/) Manipulate functions
113+
* [Go](https://vivaxy.github.io/examples/go/) Go
114+
* [Integrate Go Library into a JavaScript Webpage with WebAssembly](https://vivaxy.github.io/examples/go/integrate-go-library-into-a-javaScript-webpage-with-web-assembly/) Integrate Go Library into a JavaScript Webpage with WebAssembly
113115
* [JavaScript](https://vivaxy.github.io/examples/javascript/) JavaScript
114116
* [append script](https://vivaxy.github.io/examples/javascript/append-script/) Append script
115117
* [Arrow Function](https://vivaxy.github.io/examples/javascript/arrow-function/) Arrow Function

go/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"
8+
/>
9+
<meta name="description" content="Go" />
10+
<meta
11+
name="keywords"
12+
content="vivaxy,example,examples,github,demo,demos,playground,test,Go,samples,sample,go"
13+
/>
14+
<meta name="author" content="vivaxy" />
15+
<link type="image/png" rel="shortcut icon" href="/vivaxy.icon.png" />
16+
<link type="text/css" rel="stylesheet" href="/index.css" />
17+
<title>Go</title>
18+
</head>
19+
<body>
20+
<script src="https://unpkg.com/@vivaxy/framework/utils/console.js"></script>
21+
<script type="module" src="/index.js"></script>
22+
<script type="text/javascript" charset="utf-8" src="/cm.js"></script>
23+
</body>
24+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GOOS=js GOARCH=wasm go build -o main.wasm main.go
2+
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
3+
echo "/* eslint-disable */" | cat - wasm_exec.js > wasm_exec_disabled.js
4+
mv wasm_exec_disabled.js wasm_exec.js
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module vivaxy.github.io/examples/go/integrate-go-library-into-a-javaScript-webpage-with-web-assembly
2+
3+
go 1.22.5
4+
5+
require github.com/neurosnap/sentences v1.1.2 // indirect
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/neurosnap/sentences v1.1.2 h1:iphYOzx/XckXeBiLIUBkPu2EKMJ+6jDbz/sLJZ7ZoUw=
2+
github.com/neurosnap/sentences v1.1.2/go.mod h1:/pwU4E9XNL21ygMIkOIllv/SMy2ujHwpf8GQPu1YPbQ=
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"
8+
/>
9+
<meta
10+
name="description"
11+
content="Integrate Go Library into a JavaScript Webpage with WebAssembly"
12+
/>
13+
<meta
14+
name="keywords"
15+
content="vivaxy,example,examples,github,demo,demos,playground,test,Integrate Go Library into a JavaScript Webpage with WebAssembly,samples,sample,go,integrate-go-library-into-a-javaScript-webpage-with-web-assembly,Go"
16+
/>
17+
<meta name="author" content="vivaxy" />
18+
<link type="image/png" rel="shortcut icon" href="/vivaxy.icon.png" />
19+
<link type="text/css" rel="stylesheet" href="index.css" />
20+
<title>
21+
Integrate Go Library into a JavaScript Webpage with WebAssembly
22+
</title>
23+
</head>
24+
<body>
25+
<h1>Source article:</h1>
26+
<p>
27+
<a
28+
href="https://tderflinger.com/en/how-to-integrate-go-library-js-webpage-webassembly"
29+
target="_blank"
30+
>Integrate Go Library into a JavaScript Webpage with WebAssembly</a
31+
>
32+
</p>
33+
<textarea id="input" cols="80" rows="20"></textarea>
34+
<button id="button">&gt;&gt;&gt; Tokenize &gt;&gt;&gt;</button>
35+
<textarea id="output" cols="80" rows="20"></textarea>
36+
<script src="wasm_exec.js"></script>
37+
<script src="https://unpkg.com/@vivaxy/framework/utils/console.js"></script>
38+
<script type="module" src="index.js"></script>
39+
<script type="text/javascript" charset="utf-8" src="/cm.js"></script>
40+
</body>
41+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @since 2024-07-08
3+
* @author vivaxy
4+
*/
5+
// @ts-expect-error Go is defined globally
6+
const go = new Go();
7+
WebAssembly.instantiateStreaming(fetch('./main.wasm'), go.importObject).then(
8+
(result) => {
9+
go.run(result.instance);
10+
},
11+
);
12+
13+
document.getElementById('button').addEventListener('click', function () {
14+
const inputValue = /** @type {HTMLTextAreaElement} */ (
15+
document.getElementById('input')
16+
).value;
17+
// @ts-expect-error tokenizeSentence is defined globally
18+
const outputValue = window.tokenizeSentence(inputValue);
19+
/** @type {HTMLTextAreaElement} */ (document.getElementById('output')).value =
20+
outputValue;
21+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"syscall/js"
7+
8+
"github.com/neurosnap/sentences/english"
9+
)
10+
11+
func sentenceWrapper() js.Func {
12+
sentenceFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
13+
if len(args) != 1 {
14+
return "Invalid no of arguments passed"
15+
}
16+
inputSentence := args[0].String()
17+
fmt.Printf("input %s\n", inputSentence)
18+
tokenizer, err := english.NewSentenceTokenizer(nil)
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
sentences := tokenizer.Tokenize(inputSentence)
24+
for _, s := range sentences {
25+
fmt.Println(s.Text)
26+
}
27+
28+
var sentenceTexts []string
29+
for _, s := range sentences {
30+
trimmedText := strings.TrimSpace(s.Text)
31+
sentenceTexts = append(sentenceTexts, trimmedText)
32+
}
33+
34+
allSentences := strings.Join(sentenceTexts, "\n") // Join all sentences separated by a space
35+
return allSentences
36+
37+
})
38+
return sentenceFunc
39+
}
40+
41+
func main() {
42+
fmt.Println("Go Web Assembly")
43+
js.Global().Set("tokenizeSentence", sentenceWrapper())
44+
<-make(chan struct{})
45+
}
Binary file not shown.

0 commit comments

Comments
 (0)