goscript

package
v0.5.32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package goscript provides support for running Cogent Core code compiled to JavaScript by goscript (https://github.com/s4wave/goscript), which is used for the js platform. Code compiled by goscript is built with GOOS=js, GOARCH=wasm, and the goscript build tag.

The main use of this package is Test, which runs the existing Go tests of a package compiled to JavaScript, so that the same tests verify both the native and the goscript versions of the code. Each package does this with a test file like this:

//go:build !goscript

package mypackage

import (
	"testing"

	"cogentcore.org/core/base/goscript"
)

func TestGoScript(t *testing.T) { goscript.Test(t) }

Benchmarks can also be run in JavaScript by setting the CORE_GOSCRIPT_BENCH environment variable to a benchmark regular expression (see BenchEnvVar), for example:

CORE_GOSCRIPT_TEST=1 CORE_GOSCRIPT_BENCH=. go test -v -run TestGoScript

This package must only import the standard library, so that it can be used in the tests of any package without an import cycle.

Index

Constants

View Source
const BenchEnvVar = "CORE_GOSCRIPT_BENCH"

BenchEnvVar is the environment variable that, when set to a regular expression, makes Test also run the benchmarks matching it in JavaScript, as with go test -bench, logging their results (use go test -v to see them). The tests are not run in this case, unless Options.Run is set.

View Source
const BenchTimeEnvVar = "CORE_GOSCRIPT_BENCHTIME"

BenchTimeEnvVar is the environment variable that sets the minimum time to run each benchmark when BenchEnvVar is set, as a duration such as 2s.

View Source
const Enabled = false

Enabled is whether the code is running as JavaScript compiled by goscript.

View Source
const EnvVar = "CORE_GOSCRIPT_TEST"

EnvVar is the environment variable that must be set to a true value (such as 1) for Test to run. Otherwise, Test is skipped, because compiling and running the tests in JavaScript takes significant time, and requires goscript and bun (https://bun.sh) to be installed.

Variables

This section is empty.

Functions

func Skip

func Skip(t testing.TB, reason string)

Skip skips the given test with the given reason if the code is running as JavaScript compiled by goscript (see Enabled). It is for the (ideally rare and temporary) tests that do not work in goscript.

func Test

func Test(t *testing.T, opts ...Options)

Test runs the Go tests of the package in the current directory compiled to JavaScript by goscript, and reports any failures as failures of t. It is skipped unless EnvVar is set. It must be called from a test in a file with a !goscript build constraint, so that it does not run itself recursively (see the package documentation for an example).

Types

type Options

type Options struct {

	// Run, if non-empty, only runs the tests matching this regular expression.
	Run string

	// Timeout is the maximum time for compiling and running the tests.
	// It defaults to 5 minutes.
	Timeout time.Duration

	// Browser runs the tests in a Chromium browser instead of bun.
	Browser bool

	// BrowserChannel, if non-empty, runs the [Options.Browser] tests in
	// this installed browser, such as "chrome", instead of the Chromium
	// that Playwright downloads.
	BrowserChannel string

	// BrowserArgs are additional command-line arguments for the browser,
	// such as "--enable-unsafe-webgpu" to enable WebGPU in headless Chrome.
	BrowserArgs []string

	// Tags are additional build tags (the goscript tag is always added).
	Tags []string
}

Options are the options for Test.

Directories

Path Synopsis
Package gsbench has benchmarks of basic Go language operations, used to compare the performance of code compiled to JavaScript by goscript with native Go code (see the goscript package).
Package gsbench has benchmarks of basic Go language operations, used to compare the performance of code compiled to JavaScript by goscript with native Go code (see the goscript package).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL