js2go

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2025 License: MIT Imports: 4 Imported by: 0

README

js2go

Encode Go structs to js.Values and viceversa based on tags.

Go Report Card GitHub issues Documentation license

Usage

Assume we want to encode/decode a struct defined as follows:

type config struct {
	StringField  string   `js:"stringField"`
	IntegerField int      `js:"integerField"`
	StringsArray []string `js:"stringsArray"`
	NestedObject struct {
		InnerStringField  string   `js:"innerStringField"`
		InnerIntegerField int      `js:"innerIntegerField"`
		InnerStringsArray []string `js:"innerStringsArray"`
	} `js:"nestedObject"`
}
Decode a js.Value onto a Go struct
var c config
err := js2go.Decode(val, &c)
if err != nil {
    // handle error
}
Encode a Go struct as a js.Value:
jsValue, err := js2go.Encode(&config{
    StringField:  "hello world",
    IntegerField: 1234,
    StringsArray: []string{"hello", "world"},
})
if err != nil {
    // handle error
}

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(input js.Value, result any) error

Decode takes a js.Value and decodes it into a struct.

func Encode

func Encode(input any) (js.Value, error)

Encode takes an input struct and converts it into a js.Value.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL