Documentation
¶
Overview ¶
Package linker provides dynamic Go package binding for TypeGo.
When TypeScript code imports a Go package using the go: prefix (e.g., go:fmt), the linker is responsible for fetching, inspecting, and generating bindings for that package at compile time.
Components ¶
The linker consists of three main components:
- Fetcher: Downloads Go packages using `go get`
- Inspector: Analyzes package exports using go/packages
- Generator: Produces Go shim code and TypeScript definitions
Workflow ¶
The typical usage pattern:
// 1. Create a fetcher
fetcher, err := linker.NewFetcher()
if err != nil {
log.Fatal(err)
}
defer fetcher.Cleanup()
// 2. Fetch the package
if err := fetcher.Get("github.com/fatih/color"); err != nil {
log.Fatal(err)
}
// 3. Inspect exported functions
info, err := linker.Inspect("github.com/fatih/color", fetcher.TempDir)
if err != nil {
log.Fatal(err)
}
// 4. Generate bindings
shimCode := linker.GenerateShim(info, "pkg_color")
tsTypes := linker.GenerateTypes(info)
Package Info ¶
The Inspect function returns a PackageInfo struct containing:
- Name: The package's short name (e.g., "color")
- Path: The full import path (e.g., "github.com/fatih/color")
- Exports: A slice of FunctionInfo describing each exported function
Shim Generation ¶
GenerateShim produces Go code that binds package functions to the Goja runtime. The generated code creates a global object (e.g., _go_hyper_color) with methods that forward calls to the actual Go functions.
Type Generation ¶
GenerateTypes produces TypeScript declaration content for IDE support. The output includes JSDoc comments derived from the Go source documentation.
Index ¶
- func GenerateShim(info *PackageInfo, variableName string) string
- func GenerateTSShim(info *PackageInfo) string
- func GenerateTypes(info *PackageInfo) string
- func GoToTSType(goType string) string
- func GoToTSTypeWithStructs(goType string, knownStructs map[string]bool) string
- type ArgInfo
- type ExportedFunc
- type ExportedStruct
- type Fetcher
- type FieldInfo
- type MethodInfo
- type PackageInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateShim ¶
func GenerateShim(info *PackageInfo, variableName string) string
GenerateShim creates the Go code to bind the package to the VM. Binds both top-level functions and exported structs.
func GenerateTSShim ¶
func GenerateTSShim(info *PackageInfo) string
GenerateTSShim creates the TypeScript source for the virtual module entry point. This is used by the compiler to resolve imports like "go:github.com/..."
func GenerateTypes ¶
func GenerateTypes(info *PackageInfo) string
GenerateTypes creates the TypeScript definition with JSDoc.
func GoToTSType ¶
GoToTSType maps Go types to TypeScript equivalents.
Types ¶
type ExportedFunc ¶
ExportedFunc represents a top-level exported function.
type ExportedStruct ¶
type ExportedStruct struct {
Name string
PackagePath string // Source package import path
TypeParams []string
Doc string
Fields []FieldInfo
Methods []MethodInfo
Embeds []string // Embedded struct type names (for interface extension)
}
ExportedStruct represents an exported struct type with its fields and methods.
type Fetcher ¶
type Fetcher struct {
TempDir string
}
Fetcher handles downloading remote Go modules
func NewFetcher ¶
NewFetcher creates a new Fetcher in a temporary directory
type MethodInfo ¶
type MethodInfo struct {
Name string
Doc string
Receiver string
IsPointerRecv bool
Args []ArgInfo
Returns []string
HasCallbackArg bool
}
MethodInfo describes a method attached to a struct.
type PackageInfo ¶
type PackageInfo struct {
Name string
ImportPath string
Exports []ExportedFunc
Structs []ExportedStruct
}
PackageInfo contains metadata about an inspected Go package.