英文:
How to use custom packages
问题
我正在尝试在Go中创建和使用一个自定义包。这可能是一件非常明显的事情,但我找不到太多关于这个的信息。基本上,我在同一个文件夹中有这两个文件:
mylib.go
package mylib
type SomeType struct {
}
main.go
package main
import (
"mylib"
)
func main() {
}
当我尝试运行go run main.go
时,我得到以下错误:
main.go:4:2: import "mylib": cannot find package
我尝试先运行go build mylib.go
,但似乎没有任何反应(没有生成文件,也没有错误消息)。所以你有什么办法可以解决这个问题吗?
英文:
I'm trying to create and use a custom package in Go. It's probably something very obvious but I cannot find much information about this. Basically, I have these two files in the same folder:
mylib.go
package mylib
type SomeType struct {
}
main.go
package main
import (
"mylib"
)
func main() {
}
When I try to go run main.go
, I get this error:
main.go:4:2: import "mylib": cannot find package
I've tried to run go build mylib.go
first but it doesn't seem to be doing anything (no file generated, no error message). So any idea how I could do this?
答案1
得分: 188
首先,请确保阅读并理解《如何编写Go代码》文档。
实际的答案取决于您的“自定义包”的性质。
如果它是用于通用用途,请考虑采用所谓的“Github代码布局”。基本上,您可以将您的库作为一个独立的go get
可获取的项目。
如果您的库是用于内部使用的,可以按照以下步骤进行:
- 将包含库文件的目录放置在项目的目录下。
- 在项目的其他部分中,使用相对于包含项目的工作区根目录的路径引用该库。
举个例子:
src/
myproject/
mylib/
mylib.go
...
main.go
现在,在顶级的main.go
中,您可以使用import "myproject/mylib"
来引用该库,它将正常工作。
英文:
First, be sure to read and understand the "How to write Go code" document.
The actual answer depends on the nature of your "custom package".
If it's intended to be of general use, consider employing the so-called "Github code layout". Basically, you make your library a separate go get
-table project.
If your library is for internal use, you could go like this:
- Place the directory with library files under the directory of your project.
- In the rest of your project, refer to the library using its path relative to the root of your workspace containing the project.
To demonstrate:
src/
myproject/
mylib/
mylib.go
...
main.go
Now, in the top-level main.go
, you could import "myproject/mylib"
and it would work OK.
答案2
得分: 77
对于这种文件夹结构:
main.go
mylib/
mylib.go
最简单的方法是使用以下代码:
import (
"./mylib"
)
英文:
For this kind of folder structure:
main.go
mylib/
mylib.go
The simplest way is to use this:
import (
"./mylib"
)
答案3
得分: 7
我是一名经验丰富的程序员,但是在Go世界中还是新手!我承认我在理解Go方面遇到了一些困难...
当我尝试将我的go文件组织到子文件夹中时,我也遇到了同样的问题。
我是这样做的:
GO_Directory(分配给$GOPATH的目录)
GO_Directory //分配给$GOPATH的目录
__MyProject
_____ main.go
_____ Entites
_____ Fiboo //在我的情况下,fiboo是一个数据库名称
_________ Client.go //在我的情况下,Client是一个表名称
在文件MyProject\Entities\Fiboo\Client.go中
package Fiboo
type Client struct{
ID int
name string
}
在文件MyProject\main.go中
package main
import(
Fiboo "./Entity/Fiboo"
)
var TableClient Fiboo.Client
func main(){
TableClient.ID = 1
TableClient.name = 'Hugo'
// 在这里做你的事情
}
(我在Ubuntu 16.04上运行Go 1.9)
还有,请记住,我是Go的新手。如果我做的不好,请告诉我!
英文:
I am an experienced programmer, but, quite new into Go world ! And I confess I've faced few difficulties to understand Go...
I faced this same problem when trying to organize my go files in sub-folders.
The way I did it :
GO_Directory ( the one assigned to $GOPATH )
GO_Directory //the one assigned to $GOPATH
__MyProject
_____ main.go
_____ Entites
_____ Fiboo // in my case, fiboo is a database name
_________ Client.go // in my case, Client is a table name
On File MyProject\Entities\Fiboo\Client.go
package Fiboo
type Client struct{
ID int
name string
}
on file MyProject\main.go
package main
import(
Fiboo "./Entity/Fiboo"
)
var TableClient Fiboo.Client
func main(){
TableClient.ID = 1
TableClient.name = 'Hugo'
// do your things here
}
( I am running Go 1.9 on Ubuntu 16.04 )
And remember guys, I am newbie on Go. If what I am doing is bad practice, let me know !
答案4
得分: 6
对于在GitHub上托管的项目,人们通常会这样做:
github.com/
laike9m/
myproject/
mylib/
mylib.go
...
main.go
mylib.go
package mylib
...
main.go
import "github.com/laike9m/myproject/mylib"
...
英文:
For a project hosted on GitHub, here's what people usually do:
github.com/
laike9m/
myproject/
mylib/
mylib.go
...
main.go
mylib.go
package mylib
...
main.go
import "github.com/laike9m/myproject/mylib"
...
答案5
得分: 3
我尝试了很多方法,但最好的方法是使用go.mod并放置以下内容:
module nameofProject.com
然后,我从同一项目中导入使用以下代码:
import("nameofProject.com/folder")
这对于在任何地方创建项目非常有用。
英文:
I try so many ways but the best I use go.mod and put
module nameofProject.com
and then i import from same project I use
import("nameofProject.com/folder")
It's very useful to create project in any place
答案6
得分: 2
另一种解决方案:
将src/myproject
添加到$GOPATH中。
然后import "mylib"
将会编译。
英文:
another solution:
add src/myproject
to $GOPATH.
Then import "mylib"
will compile.
答案7
得分: 1
最后我找到了这个问题的解决方案。请注意,我有一个非常基本的编码环境,也只使用基本的东西,因为我目前是初学者。我希望以与使用官方库相同的方式使用我的库。我不需要编辑GOPATH或任何其他系统变量或路径,也不需要重新构建或安装任何东西。所以这是我的自上而下的解决方案:
- 在我的主包中,我的代码如下:
package main
import (
"mylib" // 这是我的库!!
"fmt"
)
func main() {
...
mylib.myFunc(par)
mylib.myOtherFunc(par1,par2)
...
}
- 在包的实现文件中,你必须使用完全相同的包名。我的自定义包如下所示:
package mylib
import (
"math"
"strconv"
"strings"
)
func myFunc(par int) {
...
}
func myOtherFunc(par1 string, par2 int) {
...
}
- 你必须记住正确地命名和放置所有的东西:
-
在其他包旁边创建一个与包名相同的子文件夹。你可以通过搜索例如'bufio','os'或'math'(在我的机器上它们位于\Go\src\下)来轻松找到它们。在我的情况下,它看起来像这样:
C: /Go /src /bufio /io /... /mylib /... /strings
-
将你的包文件移动到这个文件夹中。文件夹名称很重要,它必须与包名相同,这是编译器找到它的方式
-
你可以在这里有一个或多个.go文件,它们不需要命名为mylib.go,你可以使用任何名称,例如myMath.go,myString.go等。
-
我重复一遍:所有.go文件中的包名都必须是你的库名(在我的情况下是'mylib')
- 这样,以后你可以通过新的函数扩展你自己的库,它们可以在单独的.go文件中实现,唯一重要的是你必须将此文件存储在名为'mylib'的子文件夹中,并且包名必须相同。
希望对你有所帮助,最好的问候,
LA
英文:
finally I found the solution for this problem. Please note I have a very basic coding environment, also using basic stuff as I am a beginner at the moment. I wanted to use my library in the very same way as I use official libraries. I didn't have to edit GOPATH or any other system variables or paths, no need to rebuild or install anything. So here is my solution from top-down:
- In my main package I have my code as follows:
package main
import (
"mylib" // This is my library!!
"fmt"
)
func main() {
...
mylib.myFunc(par)
mylib.myOtherFunc(par1,par2)
...
}
- You have to use the very same package name in the package implementation files. My own package looks like as follows:
package mylib
import (
"math"
"strconv"
"strings"
)
func myFunc(par int) {
...
}
func myOtherFunc(par1 string, par2 int) {
...
}
- What you have to keep in mind to name and place correctly all the stuff:
-
Create a subfolder with the package name next to the other packages. You can find them easily by searchin eg. 'bufio', 'os' or 'math' (on my machine they are under \Go\src). In my case it looks like this:
C: /Go /src /bufio /io /... /mylib /... /strings
-
Move your package file into this folder. The folder name is important, this has to be the same as the package name, this is how your compiler finds it
-
You can have one ore more .go files here, they needn't to be named mylib.go, you can use any names, eg. myMath.go, myString.go, etc.
-
I repeat: the package name in all the .go files has to be your library name ('mylib' in my case)
- This way later on you can extend your own library by new functions, they can be implemented in separate .go files, the only important is you have to store this file in subfolder named 'mylib' and the package name has to be the same.
I hope it helps, best regards,
LA
答案8
得分: 0
对于那些遇到这个问题的人,你需要在使用自定义包之前先初始化go模块。
例如,你的代码目录是:../mycode/main.go
。现在你想创建diffcode
自定义包并将其导入到main中。你首先需要运行命令go mod init mycode
(确保你在../mycode
目录下)。现在你创建了diffcode
包,并且它有一些文件。要导入这个包,你需要将它放入main.go
中:module/package
。在这种情况下,是mycode/diffcode
。
英文:
For those who face this problem, you need to first initialize the go module before you can use custom package.
For example your code directory is: ../mycode/main.go
. Now you want to create diffcode
custom package and import it into main. You will first need to run the command go mod init mycode
(make sure you are under ../mycode
directory). Now you create package diffcode
and it has some files. To import this package, you need to put this into main.go
: module/package
. In this case, mycode/diffcode
.
答案9
得分: 0
这里的关键问题是将编译包作为库二进制文件进行定制,然后在一些新的Go项目中将二进制代码作为第三方库导入,类似于“net/http”或“fmt”,而不是使用原始的Go代码。我想知道这对于Go编程是否可能。
英文:
The essential question here is to tailor the idea of compiling the package as library binary, and then import the binary code as third party library like "net/http" or "fmt" in some new go project, instead using the original go code. I am wondering if this is possible or not for go programming.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论