Skip to content

Your first window ​

Run a desktop window with text and a button. You need Go 1.25 or newer and a Windows, macOS, or Linux desktop environment.

1. Create a project ​

sh
mkdir hello-dxui
cd hello-dxui
go mod init example.com/hello-dxui
go get github.com/dxui-org/dxui

2. Copy the code ​

Create main.go in the project directory and copy this complete program:

go
package main

import (
	"github.com/dxui-org/dxui"
	"log"
)

func main() {
	app := dxui.NewApp(dxui.AppOptions{
		Background: dxui.RGBA(248, 250, 252, 255),
		Title:      "Hello dxui",
		Width:      640,
		Height:     400,
	})
	if err := app.Run(func() dxui.View {
		return dxui.Box(
			dxui.BoxProps{
				Gap: 16,
				Style: dxui.Style{
					Padding: dxui.Padding(24),
				},
			},
			dxui.Label("Hello, dxui!"),
			dxui.TextButton(dxui.ButtonProps{
				OnPress: app.Close,
			}, "Close"),
		)
	}); err != nil {
		log.Fatal(err)
	}
}

3. Run it ​

In the project directory, run:

sh
go run .

If you see the Hello dxui window, it worked. Click Close to close it.

What the code does ​

CodePurpose
NewAppConfigures the window title, width, and height.
app.RunOpens the window and runs until the application closes.
BoxArranges its children vertically by default.
Label / TextButtonDisplays text / a button.
OnPressFunction called when the button is activated.

Try changing Title and the Label text, then run again. Call Run from main, not from a goroutine.

Optional: build an executable without CGO

Windows PowerShell:

powershell
$env:CGO_ENABLED = "0"
go build -o hello-dxui.exe .
./hello-dxui.exe

macOS / Linux:

sh
CGO_ENABLED=0 go build -o hello-dxui .
./hello-dxui

Keep go.mod and go.sum to record dependency versions.

If the window does not open, inspect the terminal error, then consult troubleshooting.

Next: Make a button update the UI.