Movatterモバイル変換


[0]ホーム

URL:


bundle

command
v0.37.0Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License:BSD-3-ClauseImports:14Imported by:0

Details

Repository

cs.opensource.google/go/x/tools

Links

Documentation

Overview

Bundle creates a single-source-file version of a source packagesuitable for inclusion in a particular target package.

Usage:

bundle [-o file] [-dst path] [-pkg name] [-prefix p] [-import old=new] [-tags build_constraints] <src>

The src argument specifies the import path of the package to bundle.The bundling of a directory of source files into a single source filenecessarily imposes a number of constraints.The package being bundled must not use cgo; must not use conditionalfile compilation, whether with build tags or system-specific file nameslike code_amd64.go; must not depend on any special comments, whichmay not be preserved; must not use any assembly sources;must not use renaming imports; and must not use reflection-based APIsthat depend on the specific names of types or struct fields.

By default, bundle writes the bundled code to standard output.If the -o argument is given, bundle writes to the named fileand also includes a “//go:generate” comment giving the exactcommand line used, for regenerating the file with “go generate.”

Bundle customizes its output for inclusion in a particular package, the destination package.By default bundle assumes the destination is the package in the current directory,but the destination package can be specified explicitly using the -dst option,which takes an import path as its argument.If the source package imports the destination package, bundle will removethose imports and rewrite any references to use direct references to thecorresponding symbols.Bundle also must write a package declaration in the output and mustchoose a name to use in that declaration.If the -pkg option is given, bundle uses that name.Otherwise, the name of the destination package is used.Build constraints for the generated file can be specified using the -tags option.

To avoid collisions, bundle inserts a prefix at the beginning ofevery package-level const, func, type, and var identifier in src's code,updating references accordingly. The default prefix is the package nameof the source package followed by an underscore. The -prefix optionspecifies an alternate prefix.

Occasionally it is necessary to rewrite imports during the bundlingprocess. The -import option, which may be repeated, specifies thatan import of "old" should be rewritten to import "new" instead.

Example

Bundle archive/zip for inclusion in cmd/dist:

cd $GOROOT/src/cmd/distbundle -o zip.go archive/zip

Bundle golang.org/x/net/http2 for inclusion in net/http,prefixing all identifiers by "http2" instead of "http2_", andincluding a "!nethttpomithttp2" build constraint:

cd $GOROOT/src/net/httpbundle -o h2_bundle.go -prefix http2 -tags '!nethttpomithttp2' golang.org/x/net/http2

Update the http2 bundle in net/http:

go generate net/http

Update all bundles in the standard library:

go generate -run bundle std

Notes

Bugs

  • bundle may generate incorrect codedue to shadowing between identifiers and imported package names.

    The generated code will either fail to compile or(unlikely) compile successfully but have different behaviorthan the original package. The risk of this happening is higherwhen the original package has renamed imports (they're typicallyrenamed in order to resolve a shadow inside that particular .go file).

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp