Using Go packages
Until Xo has its own ecosystem, any Go package is one line away:
gointerop/main.xo
use go "strings"
use go "net/url"
fn main(os: Os) ! {
os.stdio.println(strings.to_upper("hello from go"))
let u = url.parse("https://xolang.dev/docs?tab=tour")?
os.stdio.println("${u.hostname()} ${u.query().get("tab")}")
}Output:
HELLO FROM GO
xolang.dev tour
- A program that uses Go packages builds with the Go backend:
xo run --backend=go .(it needs the Go toolchain). Without the flag,xostops and names the package, since the default backend is native. xo check,build,run, andtestgenerate the bindings themselves.- Names follow Xo’s style:
ToUpperisto_upper. - Go’s
(T, error)becomesT ! go.Error, sourl.parse(...)?works like any Xo call, and a pointer that may be nil becomesT?. - A Go struct’s fields are methods:
le.message(),le.set_message(s), including the fields an embedded struct promotes (le.entry()is the embeddedzapcore.Entryitself, so a change through it is seen inle). - A List, Map or Set given to Go is a copy: a Go function that changes its
argument in place changes the copy, and the Xo value stays as it was.
Take the result Go returns, or use
go.out[T]()for a function that fills a value (json.unmarshal(b, out.any()), thenout.get()).xo checkwarnsXO1107where this can happen. - Every call into Go performs the
goeffect. Go code can reach files and the network without a capability, so apubfunction that calls Go must sayuses go, and that escape stays visible in signatures.
Native builds can call C the same way with use c "header.h"; see section
4.5 of the specification.
That is the tour. The specification covers everything in full, and the diagnostics reference explains every error code.