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

11,248

Solution 1

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

Solution 2

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/

Please explain your downvote

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.

Share:
11,248
Chelo Tavano
Author by

Chelo Tavano

Updated on July 02, 2022

Comments

  • Chelo Tavano
    Chelo Tavano almost 2 years

    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?