递归编译文件

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

Recursive compile files

问题

我刚刚开始学习Go,并且我喜欢它!我试图使我的项目结构更加可管理,而不是将所有内容都放在main.go中。

所以现在我的结构是这样的。

src/
-> main.go
-> routes.go
-> handlers/
--> user_handlers.go

但是当我尝试使用以下命令构建它时

go build -v -o ./bin/my_bin ./src/...

我得到了这个错误

cannot use -o with multiple packages

但是如果我将其变成一个扁平的结构,像这样

src/
-> main.go
-> routes.go
-> user_handlers.go

它就可以正常工作了,所有的文件顶部都有"package main"。

我做错了什么?

英文:

I just started with Go, and i love it! I have tried to make the structure of my project a bit more manageable, instead of having everything in my main.go

So now i have a structure like this.

src/
-> main.go
-> routes.go
-> handlers/
--> user_handlers.go

But when i try to build this with the following command

go build -v -o ./bin/my_bin ./src/...

I get this error

cannot use -o with multiple packages

But if i make it a flat structure like this

src/
-> main.go
-> routes.go
-> user_handlers.go

It works just fine, all my files got "package main" in the top of them.

What am i doing wrong?

答案1

得分: 2

包名必须与目录名匹配。将源文件移动到新目录中需要同时更改包名。

foo/foo.go // 包名为 foo
foo/bar/bar.go // 包名为 bar
foo/bar/qux.go // 包名为 bar

路径与包名无关。

包 foo: /some/path/some/where/foo

这样可以创建和导入多个名为 "foo" 的包,只要你的导入语句指定了所需的 "foo" 的位置。

附注:包名的约定是小写,不使用标点符号(例如,不使用下划线)。

英文:

The package name must match the directory name. Moving a source file to a new directory requires that you also change the package name.

foo/foo.go // package foo
foo/bar/bar.go // package bar
foo/bar/qux.go // package bar

The PATH is not relevant in terms of the package name.

Package foo: /some/path/some/where/foo 

This allows multiple "foo" packages to be created and imported provided your import specifies the desired location of "foo"

P.S. The convention for package names is lowercase, no punctuation (e.g., no _'s)

答案2

得分: -2

它告诉你你做错了什么,你不能将一个单独的包分散到多个文件夹中。

你需要设置并正确使用$GOPATH,并在routes.go中正确导入你的routes/文件夹。

一个简单的例子如下:

// routes.go
// 使用 . 可以在不使用包名前缀的情况下调用导入的函数
import . "full-path-to-routes/-relative-to-$GOPATH"

从https://golang.org/doc/code.html中可以了解到:

> GOPATH环境变量指定了你的工作区的位置。在开发Go代码时,这可能是你唯一需要设置的环境变量。
>
> 要开始使用,创建一个工作区目录并相应地设置GOPATH。你的工作区可以位于任何你喜欢的位置,但在本文档中我们将使用$HOME/go。请注意,这个路径不能与你的Go安装路径相同。

我强烈推荐阅读Effective Go

英文:

It tells you what you did wrong, you can't separate a single package over multiple folders.

You need to set and properly use $GOPATH and properly import your routes/ folder in routes.go.

A simple example of it is:

// routes.go
// the . means you can call imported functions without prefixing them with the package name
import . "full-path-to-routes/-relative-to-$GOPATH"

From https://golang.org/doc/code.html:

> The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code.
>
> To get started, create a workspace directory and set GOPATH accordingly. Your workspace can be located wherever you like, but we'll use $HOME/go in this document. Note that this must not be the same path as your Go installation.

I highly recommend reading Effective Go.

huangapple
  • 本文由 发表于 2014年9月29日 16:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/26095423.html
匿名

发表评论

匿名网友

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

确定