diff --git a/README.md b/README.md index 7d11a7f..6b5b664 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,13 @@ Gee 的设计与实现参考了Gin,[Go Gin简明教程](https://geektutu.com/p - [第四天:分组控制(Group)](https://geektutu.com/post/gee-day4.html),[Code - Github](gee-web/day4-group) - [第五天:中间件(Middleware)](https://geektutu.com/post/gee-day5.html),[Code - Github](gee-web/day5-middleware) - [第六天:HTML模板(Template)](https://geektutu.com/post/gee-day6.html),[Code - Github](gee-web/day6-template) -- [第七天:错误恢复(Panic Recover)](https://geektutu.com/post/gee-day7.html),[Code - Github](gee-web/day7-panic-recover) \ No newline at end of file +- [第七天:错误恢复(Panic Recover)](https://geektutu.com/post/gee-day7.html),[Code - Github](gee-web/day7-panic-recover) + +## WebAssembly Demo + +#### [教程地址](https://geektutu.com/post/quick-go-wasm.html) + +- [1. Hello World](demo-wasm/hello-world) +- [2. 注册函数](demo-wasm/hello-world) +- [3. 操作 DOM](demo-wasm/hello-world) +- [4. 回调函数](demo-wasm/hello-world) \ No newline at end of file diff --git a/demo-wasm/.gitignore b/demo-wasm/.gitignore new file mode 100644 index 0000000..b4d6ef2 --- /dev/null +++ b/demo-wasm/.gitignore @@ -0,0 +1,2 @@ +*.wasm +static \ No newline at end of file diff --git a/demo-wasm/callback/Makefile b/demo-wasm/callback/Makefile new file mode 100644 index 0000000..234573b --- /dev/null +++ b/demo-wasm/callback/Makefile @@ -0,0 +1,11 @@ +all: static/main.wasm static/wasm_exec.js +ifeq (, $(shell which goexec)) + go get -u github.com/shurcooL/goexec +endif + goexec 'http.ListenAndServe(`:9999`, http.FileServer(http.Dir(`.`)))' + +static/wasm_exec.js: + cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.js" static + +static/main.wasm: main.go + GO111MODULE=auto GOOS=js GOARCH=wasm go build -o static/main.wasm . \ No newline at end of file diff --git a/demo-wasm/callback/index.html b/demo-wasm/callback/index.html new file mode 100644 index 0000000..da37fb4 --- /dev/null +++ b/demo-wasm/callback/index.html @@ -0,0 +1,18 @@ + + +
+ + + + + + + + + + + \ No newline at end of file diff --git a/demo-wasm/callback/main.go b/demo-wasm/callback/main.go new file mode 100644 index 0000000..e9c137e --- /dev/null +++ b/demo-wasm/callback/main.go @@ -0,0 +1,32 @@ +// main.go +package main + +import ( + "syscall/js" + "time" +) + +func fib(i int) int { + if i == 0 || i == 1 { + return 1 + } + return fib(i-1) + fib(i-2) +} + +func fibFunc(this js.Value, args []js.Value) interface{} { + callback := args[len(args)-1] + go func() { + time.Sleep(3 * time.Second) + v := fib(args[0].Int()) + callback.Invoke(v) + }() + + js.Global().Get("ans").Set("innerHTML", "Waiting 3s...") + return nil +} + +func main() { + done := make(chan int, 0) + js.Global().Set("fibFunc", js.FuncOf(fibFunc)) + <-done +} diff --git a/demo-wasm/hello-world/Makefile b/demo-wasm/hello-world/Makefile new file mode 100644 index 0000000..234573b --- /dev/null +++ b/demo-wasm/hello-world/Makefile @@ -0,0 +1,11 @@ +all: static/main.wasm static/wasm_exec.js +ifeq (, $(shell which goexec)) + go get -u github.com/shurcooL/goexec +endif + goexec 'http.ListenAndServe(`:9999`, http.FileServer(http.Dir(`.`)))' + +static/wasm_exec.js: + cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.js" static + +static/main.wasm: main.go + GO111MODULE=auto GOOS=js GOARCH=wasm go build -o static/main.wasm . \ No newline at end of file diff --git a/demo-wasm/hello-world/index.html b/demo-wasm/hello-world/index.html new file mode 100644 index 0000000..c75710c --- /dev/null +++ b/demo-wasm/hello-world/index.html @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/demo-wasm/hello-world/main.go b/demo-wasm/hello-world/main.go new file mode 100644 index 0000000..8bcd95b --- /dev/null +++ b/demo-wasm/hello-world/main.go @@ -0,0 +1,9 @@ +// main.go +package main + +import "syscall/js" + +func main() { + alert := js.Global().Get("alert") + alert.Invoke("Hello World!") +} \ No newline at end of file diff --git a/demo-wasm/manipulate-dom/Makefile b/demo-wasm/manipulate-dom/Makefile new file mode 100644 index 0000000..234573b --- /dev/null +++ b/demo-wasm/manipulate-dom/Makefile @@ -0,0 +1,11 @@ +all: static/main.wasm static/wasm_exec.js +ifeq (, $(shell which goexec)) + go get -u github.com/shurcooL/goexec +endif + goexec 'http.ListenAndServe(`:9999`, http.FileServer(http.Dir(`.`)))' + +static/wasm_exec.js: + cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.js" static + +static/main.wasm: main.go + GO111MODULE=auto GOOS=js GOARCH=wasm go build -o static/main.wasm . \ No newline at end of file diff --git a/demo-wasm/manipulate-dom/index.html b/demo-wasm/manipulate-dom/index.html new file mode 100644 index 0000000..60f2e5a --- /dev/null +++ b/demo-wasm/manipulate-dom/index.html @@ -0,0 +1,18 @@ + + + + + + + + + + +1
+ + + \ No newline at end of file diff --git a/demo-wasm/manipulate-dom/main.go b/demo-wasm/manipulate-dom/main.go new file mode 100644 index 0000000..618cd85 --- /dev/null +++ b/demo-wasm/manipulate-dom/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "strconv" + "syscall/js" +) + +func fib(i int) int { + if i == 0 || i == 1 { + return 1 + } + return fib(i-1) + fib(i-2) +} + +var ( + document = js.Global().Get("document") + numEle = document.Call("getElementById", "num") + ansEle = document.Call("getElementById", "ans") + btnEle = js.Global().Get("btn") +) + +func fibFunc(this js.Value, args []js.Value) interface{} { + v := numEle.Get("value") + if num, err := strconv.Atoi(v.String()); err == nil { + ansEle.Set("innerHTML", js.ValueOf(fib(num))) + } + return nil +} + +func main() { + done := make(chan int, 0) + btnEle.Call("addEventListener", "click", js.FuncOf(fibFunc)) + <-done +} diff --git a/demo-wasm/register-functions/Makefile b/demo-wasm/register-functions/Makefile new file mode 100644 index 0000000..234573b --- /dev/null +++ b/demo-wasm/register-functions/Makefile @@ -0,0 +1,11 @@ +all: static/main.wasm static/wasm_exec.js +ifeq (, $(shell which goexec)) + go get -u github.com/shurcooL/goexec +endif + goexec 'http.ListenAndServe(`:9999`, http.FileServer(http.Dir(`.`)))' + +static/wasm_exec.js: + cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.js" static + +static/main.wasm: main.go + GO111MODULE=auto GOOS=js GOARCH=wasm go build -o static/main.wasm . \ No newline at end of file diff --git a/demo-wasm/register-functions/index.html b/demo-wasm/register-functions/index.html new file mode 100644 index 0000000..9cc865c --- /dev/null +++ b/demo-wasm/register-functions/index.html @@ -0,0 +1,18 @@ + + + + + + + + + + +1
+ + + \ No newline at end of file diff --git a/demo-wasm/register-functions/main.go b/demo-wasm/register-functions/main.go new file mode 100644 index 0000000..2e22e78 --- /dev/null +++ b/demo-wasm/register-functions/main.go @@ -0,0 +1,21 @@ +// main.go +package main + +import "syscall/js" + +func fib(i int) int { + if i == 0 || i == 1 { + return 1 + } + return fib(i-1) + fib(i-2) +} + +func fibFunc(this js.Value, args []js.Value) interface{} { + return js.ValueOf(fib(args[0].Int())) +} + +func main() { + done := make(chan int, 0) + js.Global().Set("fibFunc", js.FuncOf(fibFunc)) + <-done +}