简体   繁体   中英

Call function in another files

I don't know how I can use function in another file.

my project architecture:

.
├── main.go
└── src
    └── function.go

1 directory, 2 files

main.go

package main

import "src/funcrion"

func main() {
    funcrion.Display();
}

function.go

package src

import "fmt";

func Display() {
    fmt.Println("Hello World");
}

For start my project I use:

go run main.go

error:

main.go:3:8: cannot find package "src/funcrion" in any of:
    /usr/local/opt/go/libexec/src/src/funcrion (from $GOROOT)
    /Users/clementbolin/go/src/src/funcrion (from $GOPATH)

In first, I want to resolve this problem. In second time I want to know what is the best option for compile a real project with more 10 files for example, do I need to use Makefile? Or like in Rust go has package manager?

I believe the package name is src but I see you are trying to import src\\funcrion There is no such package called funcrion .

I think you should just do something like this main.go

package main

import "src"

func main() {
    src.Display();
}

or if you would like to call your src package as funcrion, then just import it like below,

import funcrion "src"

And make sure your file structure is as below and inside $GOPATH

Users
└── clementbolin
    └── go
        └── src
            ├── main.go
            └── src
                └── function.go

When you do import "src/funcrion" the go compiler will try to search for this in GOPATH and GOROOT, as you see in your error message. It does not try to search in the current folder. Since it doesn't find a package with that name in either GOPATH or GOROOT, it gives you that error.

A good way to reference "the current folder" would be using go modules. On the root of your project do $ go mod init myproject . (replace myproject for a more appropriate name for your project)

After you do that, you can use "myproject" to reference the project folder in package names, like:

package main

import "myprject/src"

func main() {
    src.Display();
}

Also note that the import goes only up to the package name and not the file name. So, in your case, you shouldn't do import myproject/src/function just because you have a function.go file inside a src package.

And since your Display function is inside the src package, you refer to it simply with src.Display() after importing the package. No need to specify the name of the file anywhere.

You can read more about go modules here: https://blog.golang.org/using-go-modules

Another tip is to not use src as a package name. In Go, it's common to not have a "src" folder, and as a package name is also not very good. For instance, see the line that reads src.Display() . Instinctively, i would read this as something like "the Display of the source", which has no meaning. But if instead of src you name your package text , that same line would read text.Display() which would read as "display some text", which is more accurate and meaningful to what the function is doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM