如何在 Go 中从另一个文件调用函数

我想在 Go 中从另一个文件调用函数。有人能帮忙吗?

test1.go

package main


func main() {
demo()
}

test2.go

package main


import "fmt"


func main() {
}


func demo() {
fmt.Println("HI")
}

如何从 test1调用 test2中的 demo

155323 次浏览

你不能有一个以上的 main在您的包。

更一般地说,在一个包中不能有多个具有给定名称的函数。

删除 test2.go中的 main并编译应用程序。 demo函数将在 test1.go中可见。

默认情况下,Go Lang 仅构建/运行上述文件。要链接所有文件,您需要在运行时指定所有文件的名称。

运行以下两个命令:

$go run test1.go test2.go. //order of file doesn't matter
$go run *.go

如果你想构建它们,你应该做类似的事情。

我也在找同样的东西。为了回答你的问题“ 如何从 test1调用 test2中的 demo?”,这里是我做它的方法。使用 go run test1.go命令运行此代码。将 test1.go 所在的 Current _ 文件夹更改为 文件夹

测试1开始

package main


import (
L "./lib"
)


func main() {
L.Demo()
}

Lib test2开始

将 test2.go 文件放在子文件夹 lib

package lib


import "fmt"


// This func must be Exported, Capitalized, and comment added.
func Demo() {
fmt.Println("HI")
}

如果你只是运行 go run test1.go,并且那个文件在同一个包中的另一个文件中有一个函数的引用,它会出错,因为你没有告诉 Go 运行整个包,你告诉它只运行那个文件。

您可以通过在以几种方式进行命令的运行中将文件分组为一个包来告诉 go 作为一个整体运行。下面是一些示例(如果终端位于软件包的目录中) :

go run ./

或者

go run test1.go test2.go

或者

go run *.go

您可以使用 build 命令预期相同的行为,并且在运行之后创建的可执行文件将作为一个分组包运行,其中文件知道彼此的函数,等等。例如:

go build ./

或者

go build test1.go test2.go

或者

go build *.go

之后,只需从命令行调用可执行文件,就会得到类似于在将所有文件作为一个整体运行时使用 run 命令的输出。例如:

./test1

或者不管您的可执行文件名在创建时碰巧被调用。

Folder Structure


duplicate
|
|--duplicate_main.go
|
|--countLines.go
|
|--abc.txt

开始

    package main


import (
"fmt"
"os"
)


func main() {
counts := make(map[string]int)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts)
f.Close()
}
}


for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}

倒数计时,开始

    package main


import (
"bufio"
"os"
)


func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
}
  1. go run ch1_dup2.go countLines.go abc.txt
  2. go run *.go abc.txt
  3. go build ./
  4. go build ch1_dup2.go countLines.go
  5. go build *.go

一个实用的、客观的、简单的快速示例:

总部,开始

package main


import "pathToProject/controllers"


func main() {
controllers.Test()
}

控制台,开始

package controllers


func Test() {
// Do Something
}

永远不要忘记: 可见的外部函数、变量和方法从大写字母开始。

即:

func test() {
// I am not Visible outside the file
}
 

func Test() {
// I am VISIBLE OUTSIDE the FILE
}

通过将另一个文件声明为模块,可以从另一个文件导入函数。将这两个文件保存在同一个项目文件夹中。 第一个文件 test1.go应该是这样的:

package main


func main() {
demo()
}

从第二个文件中删除 main 函数,因为一个包中只能存在一个 main 函数。第二个文件 test2.go应该如下所示:

package main


import "fmt"


func demo() {
fmt.Println("HI")
}

现在,在任何设置项目目录为工作目录的终端上运行以下命令: go mod init myproject. 这将在项目目录中创建一个名为 go.mod的文件。这个 mod文件的内容可能如下:

module myproject


go 1.16


现在从终端运行命令 go run .!根据需要,将从第一个文件执行演示功能! !

作为一个愚蠢的人谁不知道这是怎么回事去模块 应该说:

  1. 创建你的主机,开始
  2. 在同一个目录中把这个写到你的终端里

“你的模块名称”

  1. 创建一个新目录并进入其中
  2. 创建一个新的.go 文件,并将目录名写为包名
  3. 写任何你想写的函数; 只要注意你的函数必须以大写字母开头
  4. 回总部去

导入“ your module name/the name of your new directory”

  1. 最后,您需要的是在它后面写入包的名称和函数的名称

“新目录的名称”+ . + YourFunction ()

  1. 然后把这个写进终端

快跑。

你可以写 跑起来,跑起来代替。 有时候你不想创建一个目录,而是想创建一个新的。在同一个目录下运行文件,在这种情况下,你需要注意的是,无论你的函数是否以大写字母开始,你都应该运行所有。查查文件

* Go run *

因为

快跑,快

没用。