Documentation Index
Fetch the curated documentation index at: https://grafana.com/llms.txt
Fetch the complete documentation index at: https://grafana.com/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: https://grafana.com/docs/k6/next/extensions/create/go-js-bridge.md (append .md) or send Accept: text/markdown to https://grafana.com/docs/k6/next/extensions/create/go-js-bridge/. For the curated documentation index, use https://grafana.com/llms.txt. For the complete documentation index, use https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
About the Go-to-JS bridge
All k6 and xk6 binaries have an embedded JavaScript engine, sobek, which your test scripts run on.
You will deepen your conceptual knowledge of how your k6 extension works if you understand the bridge between Go internals and the JavaScript runtime.
Go-to-JavaScript bridge features
The bridge has a few features we should highlight:
Go method names are converted from Pascal to Camel case when accessed in JS. For example,
IsGreaterbecomesisGreater.Go field names convert from Pascal to Snake case. For example, the struct field
ComparisonResult stringbecomescomparison_resultin JS.Field names may be explicit using
jsstruct tags. For example, declaring the field asComparisonResult string `js:“result”` or hiding from JS using`js:"-"` .
Type conversion and native constructors
The JavaScript runtime transparently converts Go types like int64 to their JS equivalent.
For complex types where this is impossible, your script might fail with a TypeError, requiring you to explicitly convert
your object to a sobek.Object or sobek.Value.
func (*Compare) XComparator(call sobek.ConstructorCall, rt *sobek.Runtime) *sobek.Object {
return rt.ToValue(&Compare{}).ToObject(rt)
}The preceding snippet also demonstrates the native constructors feature from sobek, where methods can become JS constructors.
Methods with this signature can create Comparator instances in JS with new compare.Comparator().
While this is more idiomatic to JS, it still has the benefit of receiving the sobek.Runtime.
Was this page helpful?
Related resources from Grafana Labs

