在Go中构建和引用自己的本地包

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

Build and reference my own local package in Go

问题

我正在使用Google Go玩耍,非常有趣(!),但是我在包子系统方面遇到了一些问题。

我在Mac OS X Lion上运行Go 1.0.1。我也构建了一些单文件程序,没有任何问题(我还构建了一个使用html/templates的小型web应用程序,也没有任何问题,它可以编译和运行而且没有任何错误)。

我定义了一个“可重用”的包(even.go):

package even

func Even(i int) bool {
    return i % 2 == 0
}

func Odd(i int) bool {
    return i % 2 == 1
}

和一个使用该包的程序(useeven.go):

package main

import (
    "./even"
    "fmt"
)

func main() {
    a := 5
    b := 6

    fmt.Printf("%d是偶数%v?\n", a, even.Even(a))
    fmt.Printf("%d是奇数%v?\n", b, even.Odd(b))
}

但是当我使用以下命令编译“库”时:

go build even.go

什么都没有发生...没有错误,没有消息...发生了什么?

我应该如何做?

英文:

I'm playing with Google Go and I'm having fun (!), but I'm having some problems with package subsystem.

I'm running Go 1.0.1 on Mac OS X Lion. I've build also various single file programs without problems (I've also build a small webapp using html/templates without problems and it compiles and runs without any error).

I've defined a "reusable" package (even.go):

package even

func Even(i int) bool {
    return i % 2 == 0
}

func Odd(i int) bool {
    return i % 2 == 1
}

and a consumer program (useeven.go):

package main

import (
	"./even"
	"fmt"
)

func main() {
	a := 5
	b := 6

	fmt.Printf("%d is even %v?\n", a, even.Even(a))
	fmt.Printf("%d is odd %v?\n", b, even.Odd(b))
}

But when I compile the "library" using

go build even.go

I got nothing... No errors, no message... What happens?

How should I do this?

答案1

得分: 12

你的问题的答案,“我应该怎么做?”在如何编写Go代码中有解释。这是非常重要的内容,值得一看。

go build的行为可能看起来令人困惑,但对于命令行程序来说,这是常规的行为--没有输出意味着程序成功运行。那它做了什么呢?你可以在go help build中找到答案。

> ...否则,build会编译包但丢弃结果,仅作为检查包是否可以构建的检查。

你想要更多吗?当然可以。《如何编写Go代码》解释了如何做到这一点。对于你的程序的快速修复,我将解释一下go命令期望每个包和每个可执行程序都在单独的目录中。如果你只是在useeven.go的位置下创建一个名为even的目录,并将even.go移动到其中,那么go run useeven.go应该可以像你期望的那样运行。

英文:

The answer to your question, "How should I do this?" is explained in How to Write Go Code. It's really pretty important stuff and worth taking a look at.

The behavior of go build might seem puzzling, but is actually conventional for command line programs--no output means that the program ran successfully. So what did it do? For that your answer is in go help build

> ... Otherwise build compiles the packages but discards the results,
> serving only as a check that the packages can be built.

What, you wanted more? Of course. "How to Write Go Code" explains good ways of doing this. For a quick fix to your program, I'll explain that the go command expects each package and each executable program to be in a separate directory. If you just make a directory called even, immediately under the location of useeven.go, and move even.go to it, then go run useeven.go should run just as you have it.

答案2

得分: 5

go build只在构建“单个主包”时生成输出文件。如果您想为其他包输出文件,应该使用go install。这将构建并安装该包到您的pkgs目录中。

英文:

go build only generates an output file when it builds a "single main package". If you want to output a file for your other package you should use go install. That will build and install that package to your pkgs directory.

答案3

得分: 2

正如OP在评论中指出的那样,一旦将even.go放在自己的文件夹中./even/even.gogo buildgo run useeven.go就可以正常工作。在导入中的./中有一些Go魔法,它可以自动构建“本地包”,而无需将其安装在任何地方

useeven/
├── even
│     └── even.go
└── useeven.go

如果你想在没有魔法的情况下使其工作,那么库需要被安装。一个快速而简单的方法(有污染库命名空间的风险)是通过使用符号链接来注册库的源位置,如下所示(在此示例中,GOPATH~/go):

[ useeven ]
$ ln -s $(pwd)/even ~/go/src

现在,当你构建程序时,它会自动执行go get even来安装你的库的最新版本。

请注意,这并不意味着go install*可以用于该库,它只是使这样做变得不必要。


如果你不急于解决问题,还有更符合惯用方式的方法:https://stackoverflow.com/q/35480623

英文:

As noted in a comment by the OP, go build and go run useeven.go work fine, once you put even.go in its own folder: ./even/even.go. There is a bit of Go magic in the ./ (in the import) that makes the "local package" build automatically without requiring it to be installed anywhere.

useeven/
├── even
│   └── even.go
└── useeven.go

If you wanted to make this work without the magic ./, then the library need to be installed. A quick-and-dirty way (at the risk of polluting your library namespace) of making this work for your project is to simply register the library's source location by using a symbolic link like so (in this example GOPATH is ~/go):

[ useeven ]
$ ln -s $(pwd)/even ~/go/src

Now when you build the program, it automatically performs a go get even to install an up-to-date version of your library.

Note that this doesn't make go install* work for the library, it just makes doing that unnecessary.


There are more idiomatic ways of doing this, if you're not in a hurry: https://stackoverflow.com/q/35480623

答案4

得分: 0

这里没有生成输出的原因是因为你在package上运行了go build even.go命令,而不是在main的go文件上运行。在包上这样做会检查包中的错误,但因为它不是一个带有输出的主要文件,所以不会生成成功消息。如果包的内容有问题,将显示错误,否则如果编译正常,将没有输出

相反,你应该运行:

go build useeven.go

来创建二进制文件,然后执行二进制文件并从程序中获取输出:

./useeven
英文:

The reason that output is not being generated here is because you're running the

go build even.go

command on the package and not the main go file. Doing so on the package will check for errors on the package, but because it's not a main with an output, no success messages will be generated. If there are issues with the package's contents, errors will be displayed, otherwise if it compiles fine there will be no output

Instead, you should run:

go build useeven.go

To create the binary, then to execute the binary and get output from the program:

./useeven

huangapple
  • 本文由 发表于 2012年5月2日 23:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/10416752.html
匿名

发表评论

匿名网友

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

确定