linker

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 7 Imported by: 0

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

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

func GoToTSType(goType string) string

GoToTSType maps Go types to TypeScript equivalents.

func GoToTSTypeWithStructs

func GoToTSTypeWithStructs(goType string, knownStructs map[string]bool) string

GoToTSTypeWithStructs maps Go types to TypeScript equivalents, using known structs for proper type references.

Types

type ArgInfo

type ArgInfo struct {
	Name string
	Type string
}

ArgInfo describes a function/method argument.

type ExportedFunc

type ExportedFunc struct {
	Name string
	Doc  string
	Args []ArgInfo
	Ret  []string
}

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

func NewFetcher() (*Fetcher, error)

NewFetcher creates a new Fetcher in a temporary directory

func (*Fetcher) Cleanup

func (f *Fetcher) Cleanup()

Cleanup removes the temporary directory

func (*Fetcher) Get

func (f *Fetcher) Get(pkgPath string) error

Get downloading a package version using 'go get'

type FieldInfo

type FieldInfo struct {
	Name       string
	Type       string
	TSType     string
	IsNested   bool
	ImportPath string
}

FieldInfo describes a struct field.

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.

func Inspect

func Inspect(importPath string, dir string) (*PackageInfo, error)

Inspect loads and analyzes a Go package for functions, structs, and methods.

Jump to

Keyboard shortcuts

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