How to import my own function defined out of main package in Go?

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

How to import my own function defined out of main package in Go?

问题

我在Go语言方面还是比较新手,但我来自C++学派。我只想创建一个项目并将逻辑拆分到多个文件中。

在C++中,我只需要在我的main_file.cpp中加入一行:

#include "my_own_lib.hpp"

(类似于Node.js中的module.exportsrequire('relative/path/to/my-own-lib')

然后就完成了。但是在Go中,我按照相同的逻辑操作,但结果是:

$ go run main.go
main.go:4:8: open /Users/mt/Documents/Codes/go/src/github.com/mt/Apollo/tst: no such file or directory

我的文件结构如下:

main.go

package main

import "fmt"
import "./tst"

func main() {
    fmt.Println("just testing")
    tst.p()
}

tst.go

package tst

import "fmt"

func p() {
    fmt.Println("ola")
}

当然,我的文件结构是这样的:

myFolder/
   |- main.go
   |_ tst.go

有人可以告诉我正确的做法吗?

英文:

I'm pretty new on Go but I'm from the C++ school. I just want to make a project and split the logic into multiple files.

In C++ I just need to put on my main_file.cpp a single

#include "my_own_lib.hpp" 

(similar to module.exports and then require('relative/path/to/my-own-lib') in Node.js)

and that's it. In Go I followed the same logic but my result is:

$ go run main.go
main.go:4:8: open /Users/mt/Documents/Codes/go/src/github.com/mt/Apollo/tst: no such file or directory

My files:

main.go

package main

import "fmt"
import "./tst"

func main() {
	fmt.Println("just testing")
	tst.p()
}

tst.go

package tst

import "fmt"

func p() {
	fmt.Println("ola")
}

Of course my file structure is:

myFolder/
   |- main.go
   |_ tst.go

Could someone tell me what is the right way to do this?

答案1

得分: 5

如果你将tst.go文件的包设置为"package main",你就可以在不使用任何导入语句的情况下访问p()函数。

package main

func main() {
    p()
}
package main

import "fmt"

func p() {
    fmt.Println("ola")
}

通过执行以下命令,你可以构建并运行程序:

$ go build ./ && ./test
ola

我相信这符合你的要求:

我只想创建一个项目,并将逻辑分成许多不同的文件。

如果你正在构建一个程序而不是一个库,Go 的惯例是将所有文件都设置为"package main":

包"main"告诉Go编译器该包应该编译为可执行程序,而不是共享库。
http://thenewstack.io/understanding-golang-packages/

以下是一些良好的Go项目示例,它们都符合我所说的要求:

这些都是将代码分散在多个Go文件中的可执行文件,所有文件都属于"package main"。

鉴于问题描述不太清楚,你可以选择将其解释为创建一个外部包tst。但是,当他说到:

我只想创建一个项目,并将逻辑分成许多不同的文件。

他说的是许多文件,而不是许多外部包。他还说到:

当然,我的文件结构是这样的:

myFolder/
   |- main.go
   |_ tst.go

在这种情况下,我建议按照以下正确的方式进行操作:

executable/
   |- main.go (package main)
   |- a.go (package main)
   |- b.go (package main)

将应用程序分割为main、a和b。

英文:

If you set the package of tst.go to "package main" as well, you will be able to access p() without any import statements.

$ cat main.go 
package main

func main() {
    p()
}
$ cat tst.go 
package main

import "fmt"

func p() {
    fmt.Println("ola")
}
$ go build ./ && ./test 
ola

I believe this will suit your request:

>I just want to make a project and split the logic into many different files.

If you're building a program and not a library, the accepted Go convention is to have all your files as "package main":

>The package “main” tells the Go compiler that the package should compile as an executable program instead of a shared library.
http://thenewstack.io/understanding-golang-packages/

Here's some examples of well-known idiomatic Go projects which do exactly as I say:

These are executables with the code split among multiple Go files, all of which belong to "package main".

Seeing as the OP is slightly unclear, you could choose to interpret that he wants to create an external package, tst. But he's clear when he says:
>I just want to make a project and split the logic into many different files.

He says many files - not many external packages. And he also says:
>of course my file structure is:

myFolder/
   |- main.go
   |_ tst.go

My suggestion is the correct way to do things in this case:

executable/
   |- main.go (package main)
   |- a.go (package main)
   |- b.go (package main)

With the application split among main, a and b.

答案2

得分: 2

如果我正确理解了问题:

导入路径应该是完整路径,遵循$GOPATH,所以可能是'github.com/mt/Apollo/tst'

这是包代码应该存在的位置,也是包的导入路径。从文档中可以看到:

标准库的包使用简短的路径,例如"fmt"和"net/http"。对于自己的包,您必须选择一个基本路径,这个路径不太可能与将来添加到标准库或其他外部库的内容冲突。

如果您将代码保存在某个源代码库中,那么您应该使用该源代码库的根目录作为基本路径。例如,如果您在GitHub上有一个帐户github.com/user,那么这应该是您的基本路径。

我已经使其工作。以下是我所做的:

main.go

package main

import (
	"fmt"
	"github.com/mt/Apollo/tst"
)

func main() {
	fmt.Println("just testing")
	tst.P()
}

tst.go

package tst

import (
	"fmt"
)

func P() {
	fmt.Println("ola")
}

目录结构如下:

mt/Apollo/main.go

mt/Apollo/tst/tst.go

另一个需要注意的是,我将func p()中的"P"大写了。小写标识符不会被导出。更多信息请参阅这里

英文:

If I read the question correctly:

The import path should be the full path, following the $GOPATH, so likely 'github.com/mt/Apollo/tst'

This is where the package code should reside and would also be the package import path. See this from the documentation:

> The packages from the standard library are given short paths such as
> "fmt" and "net/http". For your own packages, you must choose a base
> path that is unlikely to collide with future additions to the standard
> library or other external libraries.
>
> If you keep your code in a source repository somewhere, then you
> should use the root of that source repository as your base path. For
> instance, if you have a GitHub account at github.com/user, that should
> be your base path.

I have it working. Here is exactly what I've done:

main.go

package main

import (
	"fmt"
	"github.com/mt/Apollo/tst"
)

func main() {
	fmt.Println("just testing")
	tst.P()
}

tst.go

package tst

import (
	"fmt"
)

func P() {
	fmt.Println("ola")
}

directory structure looks like:

mt/Apollo/main.go

mt/Apollo/tst/tst.go

The other thing to note is that I capitalized the "P" in func p(). Lower case identifiers are not exported. More on that here

huangapple
  • 本文由 发表于 2016年1月24日 02:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/34967358.html
匿名

发表评论

匿名网友

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

确定