gpt4 book ai didi

go install in golang 不创建子包文件

转载 作者:IT王子 更新时间:2023-10-29 02:16:55 24 4
gpt4 key购买 nike

我在golang中创建了一个名为go-orm的包,其结构如下。

go-orm
--| mine.go
--| src
-----| src.go
-----| db
--------| DBConnection.go

当我在 go-orm 目录中运行命令“go install”时,它只创建了go-orm.a 但不是 src.a 和 db.a(子目录或包)。当我检查使用 mgo 包“go install”,它为其子目录“bson”创建了 .a 文件。

我的包需要相同的功能。我需要什么改变包,使这成为可能。

编辑1

我的包在 GOPATH/src/目录中。我所有的子包(src 和 db)都存在。

最佳答案

Package build

Go Path

The Go path is a list of directory trees containing Go source code. It is consulted to resolve imports that cannot be found in the standard Go tree. The default path is the value of the GOPATH environment variable, interpreted as a path list appropriate to the operating system (on Unix, the variable is a colon-separated string; on Windows, a semicolon-separated string; on Plan 9, a list).

Each directory listed in the Go path must have a prescribed structure:

The src/ directory holds source code. The path below 'src' determines the import path or executable name.

The pkg/ directory holds installed package objects. As in the Go tree, each target operating system and architecture pair has its own subdirectory of pkg (pkg/GOOS_GOARCH).

If DIR is a directory listed in the Go path, a package with source in DIR/src/foo/bar can be imported as "foo/bar" and has its compiled form installed to "DIR/pkg/GOOS_GOARCH/foo/bar.a" (or, for gccgo, "DIR/pkg/gccgo/foo/libbar.a").

The bin/ directory holds compiled commands. Each command is named for its source directory, but only using the final element, not the entire path. That is, the command with source in DIR/src/foo/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux. The foo/ is stripped so that you can add DIR/bin to your PATH to get at the installed commands.

Here's an example directory layout:

GOPATH=/home/user/gocode

/home/user/gocode/
src/
foo/
bar/ (go code in package bar)
x.go
quux/ (go code in package main)
y.go
bin/
quux (installed command)
pkg/
linux_amd64/
foo/
bar.a (installed package object)

使用规定的目录结构,包括使用src 作为目录名。根据例子。不要使用 srcpkgbin 作为包名。

go-orm
--| mine.go
--| src <== !? Don't use src as a package name.
-----| src.go
-----| db
--------| DBConnection.go

关于go install in golang 不创建子包文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25622493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com