从同一目录中的另一个go文件中导入外部函数和逻辑。

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

Importing external functions and logic from another go file inside same directory

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始接触Go语言,到目前为止我一直很喜欢它。到目前为止,我一直在main.go文件中编写所有的应用逻辑,但是屏幕上的文本越来越多,变得相当繁琐。我无论如何都无法弄清楚如何导入位于另一个.go文件中的外部函数。以下是我尝试实现的基本示例:

main.go

package main

func main() {
     SayHello() //这是从hello.go导入的函数
}

hello.go

package hello

import "fmt"

func SayHello() {
     fmt.Println("Hello!")
}

项目结构

/
    -main.go
    -hello.go

我知道这是一个相当简单的问题,但是无论我尝试什么,都会在控制台中出现错误。在这个示例中,我想要的是将hello.go文件中的SayHello函数导出到main.go文件中,据我所知,任何导出的内容都必须以大写字母开头。整个go.mod文件和每个文件顶部的包声明让我感到困惑,我已经花了几个小时都没有解决这个问题。

英文:

I am new to golang and have been loving it so far. So far I've been writing all my applications logic inside the main.go and it is starting to get quite cumbersome with so much text on the screen. I cannot for the life of me figure out how to import external functions that are located in another .go file. Here is a basic example of what I'm trying to accomplish

main.go

package main

func main() {
     SayHello() //THIS IS THE FUNCTION IMPORTED FROM hello.go
{

hello.go

package hello

import "fmt"

func SayHello() {
     fmt.Println("Hello!")
{

project structure

/
    -main.go
    -hello.go

I know this is a fairly simple question but everything I try will result in an error in my console. All I want in this example is to export the SayHello function from the hello.go file into the main.go file, and as far as I understand anything exported must start with a capital letter. The whole go.mod file and package declaration at the top if each file confuses me and I have not been able to figure this out for hours.

答案1

得分: 2

你每个目录只能有一个包。如果你想让hello.go中的代码位于一个单独的包中,你需要将它移动到一个子目录中。

首先,这假设你已经使用go mod init <something>初始化了你的项目。为了举例说明,我们将从以下命令开始:

go mod init example

这将创建我们的go.mod文件。接下来,我们设置正确的目录结构:

.
├── go.mod
├── hello
│   └── hello.go
└── main.go

hello.go的代码是正确的(一旦你修复了你发布的代码中的语法错误)。我们需要在main.go中添加一个import语句:

package main

import "example/hello"

func main() {
    hello.SayHello() //这是从hello.go导入的函数
}

这将构建一个可执行文件,产生预期的输出:

$ go build
$ ./example
Hello!
英文:

You can only have a single package per directory. If you want the code in hello.go to live in a separate package, you need to move it into a subdirectory.

First, this assumes that you've initialized your project using go mod init &lt;something&gt;. For the purposes of this example, we'll start with:

go mod init example

This creates our go.mod file. Next, we set up the correct directory structure:

.
├── go.mod
├── hello
│&#160;&#160; └── hello.go
└── main.go

hello.go is correct as written (well, once you fix the syntax errors in your posted code). We'll need to add an import to main.go:

package main

import &quot;example/hello&quot;

func main() {
	hello.SayHello() //THIS IS THE FUNCTION IMPORTED FROM hello.go
}

This will build an executable that produces the expected output:

$ go build
$ ./example
Hello!

huangapple
  • 本文由 发表于 2022年9月13日 21:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/73703612.html
匿名

发表评论

匿名网友

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

确定