Skip to main content

Overview

The Gcore Go SDK uses the omitzero semantics from the Go 1.24+ encoding/json release for request fields.

Required Fields

Required primitive fields (int64, string, etc.) feature the tag `json:”…,required”`. These fields are always serialized, even their zero values.

Optional Fields

Optional primitive types are wrapped in a param.Opt[T]. These fields can be set with the provided constructors:
  • gcore.String(string)
  • gcore.Int(int64)
  • And similar constructors for other types
Any param.Opt[T], map, slice, struct or string enum uses the tag `json:”…,omitzero”`. Its zero value is considered omitted. The param.IsOmitted(any) function can confirm the presence of any omitzero field.

Basic Example

p := gcore.ExampleParams{
	ID:   "id_xxx",            // required property
	Name: gcore.String("..."), // optional property

	Point: gcore.Point{
		X: 0,            // required field will serialize as 0
		Y: gcore.Int(1), // optional field will serialize as 1
		// ... omitted non-required fields will not be serialized
	},

	Origin: gcore.Origin{}, // the zero value of [Origin] is considered omitted
}

Sending Null Values

Use param.Null[T]() for optional fields and param.NullStruct[T]() for struct fields when you need to explicitly send null.
To send null instead of a param.Opt[T], use param.Null[T](). To send null instead of a struct T, use param.NullStruct[T]().
p.Name = param.Null[string]()       // 'null' instead of string
p.Point = param.NullStruct[Point]() // 'null' instead of struct

param.IsNull(p.Name)  // true
param.IsNull(p.Point) // true

Extra Fields

Request structs contain a .SetExtraFields(map[string]any) method which can send non-conforming fields in the request body. Extra fields overwrite any struct fields with a matching key.
For security reasons, only use SetExtraFields with trusted data.
// In cases where the API specifies a given type,
// but you want to send something else, use [SetExtraFields]:
p.SetExtraFields(map[string]any{
	"x": 0.01, // send "x" as a float instead of int
})

Custom Values with Override

To send a custom value instead of a struct, use param.Override[T](value).
// Send a number instead of an object
custom := param.Override[gcore.FooParams](12)

Build docs developers (and LLMs) love