如何用go语言从另一个文件调用函数?

时间:2013-01-04 10:15:44

标签: go

我想在go lang中调用另一个文件中的函数,有人可以帮忙吗?

test1.go

package main

func main() {
    demo()
}

test2.go

package main

import "fmt"

func main() {
}

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

如何从test1调用test2中的demo?

8 个答案:

答案 0 :(得分:59)

您的包裹中不能有多个main

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

删除main中的test2.go并编译应用程序。 demo功能将在test1.go中显示。

答案 1 :(得分:27)

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

运行以下两个命令之一:

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

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

答案 2 :(得分:14)

我一直在找同样的事情。要回答你的问题" 如何从test1调用test2中的demo?",这就是我的方法。使用go run test1.go命令运行此代码。将 current_folder 更改为文件夹,其中test1.go为。

<强> test1.go

package main

import (
    L "./lib"
)

func main() {
    L.Demo()
}

<强> LIB \ test2.go

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

package lib

import "fmt"

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

答案 3 :(得分:1)

Folder Structure

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

duplicate_main.go

    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)
            }
        }
    }

countLines.go

    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

答案 4 :(得分:0)

如果您仅运行go run test1.go,并且该文件引用了同一软件包中另一个文件中的函数,则它将出错,因为您没有告诉Go运行整个软件包,而只是告诉它运行该文件。

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

go run ./

OR

go run test1.go test2.go

OR

go run *.go

您可以使用build命令获得相同的行为,并且在运行后,所创建的可执行文件将作为分组程序包运行,其中文件了解彼此的功能等。示例:

go build ./

OR

go build test1.go test2.go

OR

go build *.go

然后,当您将所有文件作为一个整体打包在一起运行时,只需从命令行调用可执行文件,便会获得与使用run命令类似的输出。例如:

./test1

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

答案 5 :(得分:0)

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

main.go

package main

import "pathToProject/controllers"

func main() {
    controllers.Test()
}

control.go

package controllers

func Test() {
// Do Something
}

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

即:

func test() {
    // I am not Visible outside the file
}
 
func Test() {
    // I am VISIBLE OUTSIDE the FILE
}

答案 6 :(得分:0)

您可以通过将另一个文件声明为模块来从另一个文件导入函数。将这两个文件保存在同一个项目文件夹中。 第一个文件 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 .!演示功能将根据需要从第一个文件执行!!

答案 7 :(得分:-1)

如果两个文件都在同一个文件夹中(例如test),则无需导入任何内容。两个文件都应如下所示。

test1.go

package test

func Demo1() {
    Demo()
}

test2.go

package test

import "fmt"

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

现在假设您的main.go文件位于其他文件夹(例如maindir)中

main.go

package main

import "fmt"
import "test"
func main() {
fmt.Println("HI")
    test.Demo1()

}

最后一件事不要忘记将方法的首字母大写。