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.
func Save(name string, age int, active bool) gox.Action {
...
} 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
| JavaScript | Go | JavaScript upon receiving |
|---|---|---|
string | string | string |
boolean | bool | boolean |
number whole | int, uint and variants | number or bigint |
number decimal | float32, float64 | number |
bigint | int, uint, big.Int | bigint |
Date | time.Time | Date |
Object | struct, map | Object |
Array | slice, array | Array |
null | Type that supports nil | null |
The custom types based on these types retain the same conversion rules.
String
One string from JavaScript becomes directly a string de Go.
func Greeting(name string) gox.Action {
return "Hola, " + name
} 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.
func SetActive(active bool) gox.Action {
return active
} 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.
func UpdateScore(score int, average float64) gox.Action {
...
} 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.
func Identifier() gox.Action {
return int64(9007199254740993)
} 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.
func Store(value big.Int) gox.Action {
return value
} 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.
func Schedule(date time.Time) gox.Action {
return date
} 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:
type Profile struct {
Name string
Active bool
}
func SaveProfile(profile Profile) gox.Action {
return profile
} 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:
func SaveSettings(settings map[string]string) gox.Action {
return settings
} 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.
func SaveScores(scores []int) gox.Action {
return scores
} 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.
func UpdateProfile(profile *Profile) gox.Action {
if profile == nil {
...
}
...
} 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:
| Control | Sent value |
|---|---|
HTMLInputElement text | string |
HTMLInputElement of number or range | number |
HTMLInputElement Checkbox or radio | boolean |
HTMLInputElement date | Date |
HTMLTextAreaElement | string |
HTMLSelectElement | string |
HTMLSelectElement multiple | Array of string |
HTMLInputElement file | File 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
DateIt's not valid. - One
ObjectorArrayIt can't be rebuilt with the expected guy. nullis used with a type that does not supportnil.
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
structfor known structures andmapfor dynamic keys. - Use
big.Intwhen the value can exceed the native integers. - Valid the range of numbers before storing them.
- It uses pointer only when
nullrepresents a valid state. - Keep the order of the arguments sent from JavaScript.
- Drive the conversion errors before using the result.