Skip to content

Basic syntax ​

A .gsx file combines ordinary Go with component declarations and JSX-like markup. Start here to see the shape of a file and how gsx decides whether a tag is an HTML element or a component.

Package and imports ​

Begin with a package declaration and import any packages used by your Go code. You only need to import github.com/gsxhq/gsx when you refer to runtime names such as gsx.Node, gsx.Raw, or gsx.Attrs yourself.

gsx
package views

import "github.com/gsxhq/gsx"

func wrap(n gsx.Node) gsx.Node { return n }

Declare a component ​

A component has a name, typed parameters, and a markup body. The body is the result, so it has no return type or return statement.

gsx
package views

component Greeting(name string, count int) {
	<p>Hello, { name }! You have { count } messages.</p>
}

Renders:

html
<p>Hello, World! You have 3 messages.</p>

▶ Open in Playground

The authored parameter list determines how Go and markup callers pass values. See Component signatures.

Element or component? ​

Lowercase and hyphenated tags normally render HTML elements. Capitalized and dotted tags call components, and a lowercase tag also calls a package-level declaration with the same name.

TagMeaning
<div>HTML element when div is not declared
<my-card>HTML custom element
<Card>component
<ui.Button>qualified component
<card>component when card is declared in the package

No registration is required. See Elements for element syntax.

Lowercase wrapper components ​

A lowercase component can wrap the HTML element with the same name. Inside component div, the self-named <div> stays an element instead of calling the component recursively.

gsx
package views

import "github.com/gsxhq/gsx"

component div(children gsx.Node, attrs gsx.Attrs) {
	<div { attrs... }>{ children }</div>
}

component Page() {
	<div class="card">Hello</div>
}

Renders:

html
<div class="card">Hello</div>

▶ Open in Playground

Other lowercase tags still follow the normal declaration rule. To recurse from a lowercase component, use an ordinary Go call rather than a self-named tag.

Whitespace ​

Whitespace collapses as in HTML, with one exception:

  • A whitespace run containing a newline is layout and disappears when it sits between two tags or expressions, or at the start or end of a body.
  • Every other whitespace run becomes one space, whatever sits next to it.
  • Expression values such as { " " } are emitted as written.
gsx
<p>Hello
    <em>world</em>
</p>
<span>A</span>
<span>B</span>

renders <p>Hello <em>world</em></p><span>A</span><span>B</span>.

Write content adjacent to keep it joined: { value }%, Total{ if ok { … } }. Padding inside block braces is trimmed, so a conditional suffix carries its own space: word{ if c { { " " }more } }. A source space stays even when its neighbor renders nothing. Comments and Go blocks don't split a whitespace run. pre, textarea, script and style content is verbatim.