GitHub快速開始

API 參考取自原始碼中的 Go 文件註解,以英文撰寫。

package apiref

import "github.com/nsgo-dev/game/pkg/tooling/apiref"

概覽

Package apiref reads the exported API of a Go module's packages from their source and doc comments, as go doc shows it, into a Reference: the JSON the website renders as its API pages and publishes for agents.

Load walks the module from its root and documents every importable package: it leaves out packages main, internal directories, testdata, directories starting with . or _, and nested modules. A package's files are those any NSGo target builds (linux, macos, windows, android, ios and web, without the release tag), so the API a target adds, such as one only native targets build, is in the reference with the targets that have it in Platforms.

Every exported identifier has an anchor, the fragment of its place on its package's page, as pkg.go.dev names them: Name for a constant, variable, function or type, Type.Name for a method, a struct field or an interface method.

索引

常數

const Version = 1

Version is the version of the Reference format, in its version field. A change that removes or renames a field, or changes what one holds, raises it.

變數

var Targets = []Target{
	{Name: "linux", GOOS: "linux", GOARCH: "amd64"},
	{Name: "macos", GOOS: "darwin", GOARCH: "arm64"},
	{Name: "windows", GOOS: "windows", GOARCH: "amd64"},
	{Name: "android", GOOS: "android", GOARCH: "arm64"},
	{Name: "ios", GOOS: "ios", GOARCH: "arm64"},
	{Name: "web", GOOS: "js", GOARCH: "wasm"},
}

Targets are the targets Load reads each package for, in the order Platforms lists them. Where two targets' files declare the same identifier, as a native and a web implementation of one function do, the reference shows the declaration of the earlier target.

函式

func Marshal(ref *Reference) ([]byte, error)

Marshal encodes ref as indented JSON ending in a newline. The output depends only on ref, so the same source always gives the same bytes.

型別

type Func struct {
	Name   string `json:"name"`
	Anchor string `json:"anchor"`
	// Recv is a method's receiver type as written, such as *Batch, and empty for a function.
	Recv string `json:"recv,omitempty"`
	// Decl is the signature, without the body.
	Decl      string   `json:"decl"`
	Doc       string   `json:"doc"`
	DocHTML   string   `json:"docHTML,omitempty"`
	File      string   `json:"file"`
	Line      int      `json:"line"`
	Platforms []string `json:"platforms,omitempty"`
}

Func is a function or a method.

type Ident struct {
	Name   string `json:"name"`
	Anchor string `json:"anchor"`
}

Ident is an exported identifier and its anchor.

type Member struct {
	Name   string `json:"name"`
	Anchor string `json:"anchor"`
	// Kind is "field" or "method".
	Kind string `json:"kind"`
	// Doc is the member's comments, the one above it then the one after it on its line.
	Doc string `json:"doc"`
}

Member is a struct field or an interface method.

type Options struct {
	// HTML renders each doc comment as HTML into its DocHTML. A doc link to an identifier of the same package is its anchor, to another package of the module the path to that package's page, and to any other package its page on pkg.go.dev.
	HTML bool
}

Options are what Load adds to the reference.

type Package struct {
	ImportPath string `json:"importPath"`
	Name       string `json:"name"`
	// Dir is the package's directory below the module's root, with slashes, "." for the root.
	Dir string `json:"dir"`
	// Page is the path of the package's page below the reference's root: Dir, or Name for the module's root package. DocHTML links another package of the module by the path from one page to the other, each page a directory.
	Page string `json:"page"`
	// Synopsis is the first sentence of Doc.
	Synopsis string `json:"synopsis"`
	// Doc is the package's doc comment as written, without the comment markers.
	Doc string `json:"doc"`
	// DocHTML is Doc rendered as HTML, when Options.HTML is set.
	DocHTML string `json:"docHTML,omitempty"`
	// Platforms are the targets that build the package, empty when every target does.
	Platforms []string `json:"platforms,omitempty"`
	Consts    []Value  `json:"consts"`
	Vars      []Value  `json:"vars"`
	Funcs     []Func   `json:"funcs"`
	Types     []Type   `json:"types"`
}

Package is the exported API of one package, grouped as go doc groups it: a type's constants, variables, constructors and methods are under the type rather than the package.

type Reference struct {
	// Version is the format's version, Version.
	Version int `json:"version"`
	// Module is the module's path, from its go.mod.
	Module string `json:"module"`
	// Packages are the module's importable packages, by import path.
	Packages []Package `json:"packages"`
}

Reference is the exported API of a module's packages.

func Load(root string, opts Options) (*Reference, error)

Load reads the reference of the module whose go.mod is in root.

func Unmarshal(data []byte) (*Reference, error)

Unmarshal decodes a reference Marshal wrote, refusing unknown fields and any version but Version.

type Target struct {
	Name   string
	GOOS   string
	GOARCH string
}

Target is an NSGo target a package's files are chosen for: its name as the nsgo tool spells it and the GOOS and GOARCH it builds for.

type Type struct {
	Name   string `json:"name"`
	Anchor string `json:"anchor"`
	// Decl is the type's declaration with its exported fields or methods and their comments.
	Decl      string   `json:"decl"`
	Doc       string   `json:"doc"`
	DocHTML   string   `json:"docHTML,omitempty"`
	File      string   `json:"file"`
	Line      int      `json:"line"`
	Platforms []string `json:"platforms,omitempty"`
	// Members are a struct's exported fields, embedded ones by their type's name, or an interface's methods.
	Members []Member `json:"members"`
	// Consts and Vars are the declarations of values of the type.
	Consts []Value `json:"consts"`
	Vars   []Value `json:"vars"`
	// Funcs are the functions that return the type.
	Funcs []Func `json:"funcs"`
	// Methods are the type's exported methods, with those promoted from an unexported embedded type.
	Methods []Func `json:"methods"`
}

Type is a type with the declarations go doc lists under it.

type Value struct {
	// Idents are the exported names the declaration declares, in order.
	Idents []Ident `json:"idents"`
	// Decl is the declaration's source, gofmt-formatted, with the comments on its specs.
	Decl    string `json:"decl"`
	Doc     string `json:"doc"`
	DocHTML string `json:"docHTML,omitempty"`
	// File is the path of the declaration's file below the module's root, with slashes, and Line its line.
	File string `json:"file"`
	Line int    `json:"line"`
	// Platforms are the targets that declare it, empty when every target that builds the package does.
	Platforms []string `json:"platforms,omitempty"`
}

Value is a declaration of constants or variables: one or a parenthesized group, which shares a doc comment.