Coder Social home page Coder Social logo

jianghaimingc / handytools-go-webassembly Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xd-deng/handytools-go-webassembly

0.0 0.0 0.0 209 KB

Handy Tools: Written in Golang + WebAssembly + Vue.js

Home Page: https://handytools.xd-deng.com

Dockerfile 4.60% Go 50.80% JavaScript 42.51% CSS 2.09%

handytools-go-webassembly's Introduction

Handy Tools: Written in Golang + WebAssembly + Vue.js

This application is mainly meant to test & demo how to use WebAssembly with Go.

The key steps include:

  • Compile Go application (e.g. wasm/wasm_main.go in this app) into executable WebAssembly module file, in which you can

    • specify Go functions which can be accessed from JavasScript environment later as JS functions.
    • Operate the objects in JavaScript environment.

    Note that you can only compile main packages, otherwise you get an object file that cannot be run in WebAssembly [1].

  • Copy the JavaScript support file GOROOT/misc/wasm/wasm_exec.js to your project, and load it properly in your HTML page (ensure the version of Go you use to build is consistent with the version of Go from whose GOROOT you copy from).

A live demo can be found at https://handytools.xd-deng.com

开发这个应用的主要目的是测试、学习以及展示如何在Go中使用WebAssembly。

关键步骤包括:

  • 编译Go程序(例如这里的wasm/wasm_main.go),并生成一个可执行的WebAssembly文件,在其中我们可以:

    • 用Go实现函数,并最终在JavaScript环境中将它们作为JavaScript函数进行调用
    • 对JavaScript环境中的objects进行操作

    需要注意的是,你只能够编译main packages,否则编译出的object文件将不能在WebAssembly中执行[1]。

  • 将Go提供的JavaScript支持文件GOROOT/misc/wasm/wasm_exec.js复制到你的项目中,同时在你的HTML文件中该js文件(这里GOROOT的Go版本应与你之前用来编译wasm文件的Go版本保持一致)。

Demo请移步https://handytools.xd-deng.com

Commands to Build and Run

# Compile wasm file
cd wasm
GOARCH=wasm GOOS=js go build -o index.wasm wasm_main.go
cd ..

# Copy the wasm_exec.js file from GOROOT, to double-ensure consistency
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" static/js/wasm_exec.js

# Start the service
go run main.go

Build & Run with Docker

docker build -t xddeng/handytools-wasm .
docker run -p 8000:8000 xddeng/handytools-wasm

Check in Browser Console

Once the application is running, you can open the corresponding link in your browser (or directly visit the demo https://handytools.xd-deng.com) and open the console as well.

The functions we create in wasm/wasm_main.go and expose in the main() function are accessible in the browser console, and we can invoke them in exactly the same way how we invoke JavaScript functions.

drawing

Reference

handytools-go-webassembly's People

Contributors

xd-deng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.