Giter VIP home page Giter VIP logo

vert's Introduction

vert

Go Reference Test

Package vert provides WebAssembly interop between Go and JS values.

Install

GOOS=js GOARCH=wasm go get github.com/norunners/vert

Examples

Hello World!

Below is a trivial string value interop.

package main

import "github.com/norunners/vert"

func main() {
	v := vert.ValueOf("Hello World!")
	// Use v as a JS value.

	s := ""
	v.AssignTo(&s)
	// Use s as a Go value.
}

Structs & Objects

Go structs and JS objects interop seamlessly.

package main

import "github.com/norunners/vert"

type Data struct {
	Message string
}

func main() {
	v := vert.ValueOf(Data{
		Message: "Hello World!",
	})
	// e.g. {"Message": "Hello World!"}

	d := &Data{}
	v.AssignTo(d)
}

Tagged Struct Fields

Tagged struct fields allow defined JS field names.

package main

import "github.com/norunners/vert"

type Data struct {
	Message string `js:"msg"`
}

func main() {
	v := vert.ValueOf(Data{
		Message: "Hello World!",
	})
	// The defined JS tag names the field.
	// e.g. {"msg": "Hello World!"}

	d := &Data{}
	v.AssignTo(d)
}

Error Handling

AssignTo returns an error value.

package main

import "github.com/norunners/vert"

type Data struct {
	Message string
}

func main() {
	v := vert.ValueOf("Hello World!")

	d := &Data{}
	if err := v.AssignTo(d); err != nil {
		// Handle error.
	}
}

Why?

Package syscall/js, of the Go standard library, has limited interop support between Go and JS values.

  1. The function js.ValueOf will not accept struct types. The result panics with ValueOf: invalid value.
  2. The type js.Value. does not support an Interface or assignment method for non-basic Go values. However, the methods Bool, Int, Float and String support basic Go values.

Package vert leverages and extends syscall/js to accommodate these shortcomings.

License

vert's People

Contributors

norunners avatar nitram509 avatar hustlelikeaboss 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.