Values

Communication explains the system shared by Pages, Actions and WebSocket. This page describes how GOX converts basic and structured values between JavaScript and Go using the function or handler signature.

gox
func Save(name string, age int, active bool) gox.Action {
    ...
}
js
const [result, err] = await Actions.Save("George", 30, true)

The arguments should be sent in the same order in which they appear in the signature. Action Parameters to know how this contract is applied to an Action.


Equivalence

JavaScriptGoJavaScript upon receiving
stringstringstring
booleanboolboolean
number wholeint, uint and variantsnumber or bigint
number decimalfloat32, float64number
bigintint, uint, big.Intbigint
Datetime.TimeDate
Objectstruct, mapObject
Arrayslice, arrayArray
nullType that supports nilnull

The custom types based on these types retain the same conversion rules.


String

One string from JavaScript becomes directly a string de Go.

gox
func Greeting(name string) gox.Action {
    return "Hola, " + name
}
js
const [message, err] = await Actions.Greeting("George")

GOX does not interpret content as JSON, HTML or any other type.


Boolean

One boolean JavaScript becomes bool.

gox
func SetActive(active bool) gox.Action {
    return active
}
js
const [active, err] = await Actions.SetActive(true)

Values of other types are not implicitly converted to bool.


Number

One number can be linked to a int, uint, float or one of its specific sizes.

gox
func UpdateScore(score int, average float64) gox.Action {
    ...
}
js
const [result, err] = await Actions.UpdateScore(100, 95.5)

The value must be finite. NaN, Infinity and -Infinity are rejected before being sent.

When the destination is an integer, the value must be integer, respect the sign and be within the range of the declared type. GOX does not round decimal numbers or allow overflows.

Integers received in JavaScript

The Go integers are included between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER They come as number.

The values outside that range come as bigint to preserve its accuracy.

gox
func Identifier() gox.Action {
    return int64(9007199254740993)
}
js
const [identifier, err] = await Actions.Identifier()

console.log(typeof identifier) // "bigint"

BigInt

One bigint can be received by a compatible go integer or by big.Int.

gox
func Store(value big.Int) gox.Action {
    return value
}
js
const [value, err] = await Actions.Store(123456789012345678901234567890n)

If the fate is int, uint or a fixed-size variant, the value must be within its range. big.Int when the range is not known in advance.


Date

One Date valid becomes time.Time.

gox
func Schedule(date time.Time) gox.Action {
    return date
}
js
const [date, err] = await Actions.Schedule(new Date("2026-08-04T18:00:00Z"))

GOX conserves the instant with an RFC 3339 representation. Date invalid is rejected before being sent.

WebSocket rebuilds the values time.Time how DateIn an Action response, the representation depends on the return contract used by the Action.


Object

One Object can become struct or map.

Struct

It uses a struct where the form is known:

gox
type Profile struct {
    Name   string
    Active bool
}

func SaveProfile(profile Profile) gox.Action {
    return profile
}
js
const [profile, err] = await Actions.SaveProfile({
    Name: "George",
    Active: true,
})

The exported fields participate in the conversion. The JSON tags declared by the type are respected when the conversion uses JSON.

Map

It uses a map when the keys are dynamic:

gox
func SaveSettings(settings map[string]string) gox.Action {
    return settings
}
js
const [settings, err] = await Actions.SaveSettings({
    language: "es",
    theme: "dark",
})

Each key and each value must be able to be converted to the types declared by the map.


Array

One Array can become slice or array.

gox
func SaveScores(scores []int) gox.Action {
    return scores
}
js
const [scores, err] = await Actions.SaveScores([90, 95, 100])

Each element must be compatible with the type contained. When the destination is a array, the length should also match.

File arrays follow special rules and are explained in Files.


Null

null You can only turn to a go guy who admits nil, as:

  • Pointer.
  • map.
  • slice.
  • Interface.
  • Function.
  • Canal.
gox
func UpdateProfile(profile *Profile) gox.Action {
    if profile == nil {
        ...
    }
    ...
}
js
const [result, err] = await Actions.UpdateProfile(null)

null cannot be directly linked to string, bool, int, float, struct or array.

When Go sends a value nil compatible, JavaScript receives null.


HTML controls

Actions and WebSocket can convert HTML controls used as arguments:

ControlSent value
HTMLInputElement textstring
HTMLInputElement of number or rangenumber
HTMLInputElement Checkbox or radioboolean
HTMLInputElement dateDate
HTMLTextAreaElementstring
HTMLSelectElementstring
HTMLSelectElement multipleArray of string
HTMLInputElement fileFile or File[]

The buttons cannot be used as values. An empty or invalid control can cause an error before starting communication.

The invocation of an Action with a full form is explained in Forms.


Conversion errors

GOX rejects the argument when:

  • The amount does not match the signature.
  • The rate received does not correspond to the rate declared.
  • A number is not finite or overflows fate.
  • One Date It's not valid.
  • One Object or Array It can't be rebuilt with the expected guy.
  • null is used with a type that does not support nil.

The function or handler does not run when validation fails. In WebSocket, these problems are communicated by the model described in WebSocket events.

The bytes and streams follow different rules of these conversions. Binary data to know those values.


Best practices

  • It uses the most concrete type possible in each signature.
  • Preference struct for known structures and map for dynamic keys.
  • Use big.Int when the value can exceed the native integers.
  • Valid the range of numbers before storing them.
  • It uses pointer only when null represents a valid state.
  • Keep the order of the arguments sent from JavaScript.
  • Drive the conversion errors before using the result.

See also

Next step

Continue with Binary data to transfer bytes and progressive responses.

Binary data