go语言的开发分支(通常称为go tip)包含了最新的功能、改进和实验性包。当从源代码编译go时,其构建系统会根据特定的配置来决定哪些包会被编译并包含在最终的go标准库中。对于一些处于实验阶段(如exp/目录下的包)或不常被默认编译的包,仅仅运行 ./all.bash 或 make.bash 可能无法将其包含进来,因为构建脚本默认可能只包含其子包或不包含该包本身。
例如,exp/regexp包在某些情况下可能不会被默认编译。要确保这类包能够被正确编译并随Go发行版一起安装,我们需要手动调整构建配置。
Go语言的构建系统通过 Makefile 文件来管理包的编译和链接。对于标准库中的包,src/pkg/Makefile 文件扮演着核心角色,它定义了 DIRS 变量,该变量列出了所有需要被编译到Go标准库中的包目录。因此,要将额外的包集成到Go的构建过程中,最直接有效的方法就是修改此文件。
操作步骤:
cd $GOROOT/src/pkg ls -l Makefile
vim Makefile # 或者 nano Makefile, code Makefile 等
DIRS=\ archive/tar\ archive/zip\ asn1\ bufio\ bytes\ ...
DIRS=\ archive/tar\ archive/zip\ asn1\ ... exp/regexp\ # 在这里添加 exp/regexp ... vendor/golang.org/x/net/dns/dnsmessage\ vendor/golang.org/x/text/unicode/norm\ vendor/golang.org/x/tools/go/analysis/passes/copylock
注意: 确保每行末尾都有反斜杠 \,除了 DIRS 列表的最后一行。
cd $GOROOT/src ./all.bash # 或者 ./make.bash,取决于你的系统和Go版本
这个命令会重新编译整个Go工具链和标准库,包括你刚刚添加到 Makefile 中的新包。编译完成后,exp/regexp 包就会作为标准库的一部分被安装到你的Go环境中。
以下是 src/pkg/Makefile 中 DIRS 变量修改的简化示例,展示了如何添加 exp/regexp:
# Copyright 2011 The Go Authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. include ../Make.inc # All packages (directories) # This list is sorted alphabetically. # When adding a new package, add it to the list in alphabetical order. # If the package is in a subdirectory, add the full path (e.g., cmd/go). # If the package is a vendor package, add the full path (e.g., vendor/golang.org/x/text/unicode/norm). DIRS=\ archive/tar\ archive/zip\ asn1\ bufio\ bytes\ compress/bzip2\ compress/flate\ compress/gzip\ compress/lzw\ compress/zlib\ container/heap\ container/list\ container/ring\ context\ crypto\ crypto/aes\ crypto/cipher\ crypto/des\ crypto/dsa\ crypto/ecdsa\ crypto/ed25519\ crypto/elliptic\ crypto/hmac\ crypto/internal/boring\ crypto/internal/edwards25519\ crypto/internal/nistec\ crypto/md5\ crypto/rand\ crypto/rc4\ crypto/rsa\ crypto/sha1\ crypto/sha256\ crypto/sha512\ crypto/subtle\ crypto/tls\ crypto/x509\ crypto/x509/pkix\ database/sql\ database/sql/driver\ debug/dwarf\ debug/elf\ debug/gosym\ debug/macho\ debug/pe\ debug/plan9obj\ encoding\ encoding/ascii85\ encoding/base32\ encoding/base64\ encoding/binary\ encoding/csv\ encoding/gob\ encoding/hex\ encoding/json\ encoding/pem\ encoding/xml\ errors\ exp/regexp\ # <-- 在这里添加 exp/regexp exp/regexp/syntax\ flag\ fmt\ go/ast\ go/build\ go/constant\ go/doc\ go/format\ go/importer\ go/internal/gccgoimporter\ go/internal/srcimporter\ go/internal/typeparams\ go/parser\ go/printer\ go/scanner\ go/token\ go/types\ hash\ hash/adler32\ hash/crc32\ hash/crc64\ hash/fnv\ html\ html/template\ image\ image/color\ image/draw\ image/gif\ image/jpeg\ image/png\ index/suffixarray\ internal/abi\ internal/bytealg\ internal/cpu\ internal/execabs\ internal/fmtsort\ internal/lazytemplate\ internal/nettrace\ internal/oserror\ internal/profile\ internal/race\ internal/reflectlite\ internal/singleflight\ internal/syscall/execenv\ internal/syscall/unix\ internal/testlog\ internal/unsafeheader\ internal/xcoff\ io\ io/fs\ io/ioutil\ log\ log/syslog\ math\ math/big\ math/bits\ math/cmplx\ math/rand\ mime\ mime/multipart\ mime/quotedprintable\ net\ net/http\ net/http/cgi\ net/http/cookiejar\ net/http/fcgi\ net/http/httptest\ net/http/httptrace\ net/http/httputil\ net/http/internal\ net/http/pprof\ net/mail\ net/rpc\ net/rpc/jsonrpc\ net/smtp\ net/textproto\ net/url\ os\ os/exec\ os/signal\ os/user\ path\ path/filepath\ plugin\ reflect\ regexp\ regexp/syntax\ runtime\ runtime/cgo\ runtime/debug\ runtime/metrics\ runtime/pprof\ runtime/race\ runtime/trace\ sort\ strconv\ strings\ sync\ sync/atomic\ syscall\ testing\ testing/fstest\ testing/iotest\ testing/quick\ text/scanner\ text/tabwriter\ text/template\ text/template/parse\ time\ time/tzdata\ unicode\ unicode/utf16\ unicode/utf8\ unsafe\ vendor/golang.org/x/net/dns/dnsmessage\ vendor/golang.org/x/text/unicode/norm\ vendor/golang.org/x/tools/go/analysis/passes/copylock
通过简单地编辑 src/pkg/Makefile 文件并重新编译Go,开发者可以灵活地定制Go的构建内容,确保将特定的实验性或非默认包集成到Go标准库中。这对于希望探索Go最新功能或贡献Go核心开发的开发者来说,是一个非常实用的技巧。在操作过程中,务必注意实验性包的特性和构建过程的潜在影响。
以上就是Go Tip版本编译:扩展标准库包的集成方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号