在Golang中使用”go install”命令时,不会创建子包的归档文件。

huangapple go评论82阅读模式
英文:

go install in golang not creating sub package archives

问题

我已经在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)都存在。

英文:

I have created a package named go-orm in golang and its structure is follows.

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

When I ran the command "go install" in go-orm directory, it created only
go-orm.a but not src.a and db.a (sub directories or packages). When I checked
"go install" with mgo package it created .a files for it's sub directory "bson".

I need the same functionality for my package. What change is needed in my
package to make this possible.

Edit 1

my package is in GOPATH/src/ directory. All my sub packages(src and db) exist.

答案1

得分: 2

> 构建包
>
> Go路径
>
> Go路径是一个包含Go源代码的目录树列表。它用于解析在标准Go目录树中找不到的导入。默认路径是GOPATH环境变量的值,根据操作系统解释为路径列表(在Unix上,变量是一个以冒号分隔的字符串;在Windows上,是一个以分号分隔的字符串;在Plan 9上,是一个列表)。
>
> Go路径中列出的每个目录都必须具有规定的结构:
>
> src/目录保存源代码。在'src'下的路径决定了导入路径或可执行文件名。
>
> pkg/目录保存已安装的包对象。与Go目录树一样,每个目标操作系统和架构对都有自己的pkg子目录(pkg/GOOS_GOARCH)。
>
> 如果DIR是Go路径中列出的目录,那么具有源代码在DIR/src/foo/bar中的包可以作为"foo/bar"导入,并且其编译形式被安装到"DIR/pkg/GOOS_GOARCH/foo/bar.a"(对于gccgo,是"DIR/pkg/gccgo/foo/libbar.a")。
>
> bin/目录保存编译后的命令。每个命令的名称都是其源目录,但只使用最后一个元素,而不是整个路径。也就是说,具有源代码在DIR/src/foo/quux中的命令被安装到DIR/bin/quux,而不是DIR/bin/foo/quux。去掉了foo/,这样你可以将DIR/bin添加到PATH中以访问已安装的命令。
>
> 这是一个示例目录布局:
>
> GOPATH=/home/user/gocode
>
> /home/user/gocode/
> src/
> foo/
> bar/ (包bar中的Go代码)
> x.go
> quux/ (包main中的Go代码)
> y.go
> bin/
> quux (已安装的命令)
> pkg/
> linux_amd64/
> foo/
> bar.a (已安装的包对象)

按照规定的目录结构使用,包括使用src作为目录名。请参考示例。不要将srcpkgbin用作包名。

go-orm
--| mine.go
--| src     <== !? 不要将src用作包名。
-----| src.go
-----| db
--------| DBConnection.go
英文:

> 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)

Use the prescribed directory structure, including the use of src as a directory name. Follow the example. Don't use src, pkg, or bin as package names.

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

答案2

得分: -1

经过长时间的分析,我发现在根目录的go文件中的"import"语句可以解决问题。我在根目录(go-orm)中创建了一个额外的文件,命名为create_archieve.go。在其中,我只插入了以下几行代码,它们创建了go-orm.a、src.a和db.a。

create_archieve.go

package nosql_orm

import _ "go-orm/src/db"

英文:
After long analysis I found that "import" statement in root directory go file

can do the trick. I created one extra file named create_archieve.go in root
directory(go-orm). Inside that I just inserted the following lines which created
go-orm.a, src.a and db.a.

create_archieve.go

package nosql_orm

import _"go-orm/src/db"

huangapple
  • 本文由 发表于 2014年9月2日 19:37:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/25622493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定