Using Go
Use any Go package from OSL with a go: import, pass values both ways, and read the Go that OSL generates.
OSL compiles to Go, so the whole Go ecosystem is part of OSL's. Import any Go package with go: in front of its path, and call it as you would in Go. There are no bindings to write.
import "go:strings"
import "go:unicode"
log strings.ToUpper("quiet")
log strings.Count("banana", "a")
log strings.Fields(" spread out words ")
log unicode.IsDigit(55)Refer to the package by its Go name, usually the last part of its path: go:net/http is http, go:path/filepath is filepath.
Values cross over#
OSL values convert to the Go types a function expects, and Go results come back as OSL values. Strings stay strings, numbers convert to the right Go number type, and arrays become Go slices, array literals included:
import "go:strings"
import "go:slices"
import "go:math"
def largest(int[] values) int -> slices.Max(values)
log strings.Join(["a", "b", "c"], "-")
log largest([3, 9, 4])
log math.Hypot(3, 4)Go types#
Go types work as OSL types, for variables, parameters and return values. Read and set exported fields directly; lowercase field names map to Go's exported ones:
import "go:image"
image.Point p = image.Pt(1, 2)
p.X = 5
log p.X p.Y
image.Point q = image.Point(x = 10, y = 20)
log q.Add(p).String()A Go function's result keeps its Go type, so you can call its methods straight away, as q.Add(p).String() does.
Pointers to Go types are written with *, as in *strings.Builder.
Modules from outside the standard library#
Packages beyond Go's standard library come from Go modules. Add them to your project with Opal, which records them in go.mod:
opal add go:github.com/google/uuidimport "go:github.com/google/uuid"
log uuid.NewString()Reading the generated Go#
osl transpile file.osl prints the Go that OSL generates. Every example on this site has a Go button that shows it. It's a good way to see what your code costs: typed OSL becomes plain, direct Go.
osl transpile main.oslHow do you call Go's strings.TrimSpace from OSL?
Split and shout
Use Go's strings package to split the text at each comma and print the pieces joined with | , in capitals.
PEAR | FIG | APPLE