12

Go noob, I cannot seem to figure out how to structure my project with packages. What I want is this:

  • I want to create a package, lets say its called Dart.
  • I have a file called dart.go with package main and the main function in my project directory.
  • I have another file, lets call it functions.go in my project directory with 'package dart' as the first line.
  • I just want to call functions from functions.go in main, but cannot figure out how to name the packages to get it to build.
  • If I put package dart at the top of functions.go it wont build because it finds packages main and dart. I dont want functions.go to be part of another package, I just want one package and the ability to split the functions in this package into multiple files.
  • Is this possible in go, or do I have to make multiple packages?

dart.go

package main 

import (
  ...
)  

func main () {
  ...
  // call functions declared in functions.go
  ...
}

functions.go

package dart  

import (
  ...
)

func Function1() {
  ... 
}

func Function2() {
  ...
}
bitwitch
  • 467
  • 1
  • 5
  • 15
  • 4
    These issues are covered in [How to Write Go Code](https://golang.org/doc/code.html). – Charlie Tumahai May 21 '19 at 18:52
  • 1
    Thanks, I did read this and other resources. The issue for me was that I incorrectly believed 'package main' to be a special declaration used only once per package. – bitwitch May 21 '19 at 19:49

3 Answers3

22

if all you want to do is access functions in a different file, have functions.go also start with package main instead of package dart. That way, you are working within a single package but with your code divided into multiple files. Make sure they're in the same directory so that they're considered in the same package.

Wombologist
  • 536
  • 3
  • 5
  • 5
    I think I had the preconceived idea that there could be only one package main declaration per package. Silly, this prevented me from seeing something so simple. Thank you. – bitwitch May 21 '19 at 19:07
  • 3
    For anyone whose dart.go doesn't seem to have access to Function1 like mine did: Use `go run .` instead of `go run dart.go`. See [this](https://stackoverflow.com/questions/23695448/how-to-run-all-go-files-within-current-directory-through-the-command-line-mult) explanation – Josiah Plett Jun 16 '22 at 02:22
4

If you are just looking to create a library then you won't need a main package. However, if you are looking to create a standalone program that runs functions from a different package (dartlib) then you'll need a main file.

It would also be a good idea to name your program something different than the library you are calling (program dart calling library dartlib)

Library

Your library directory structure should look something like the following:

dartlib
|
 dartlib.go

dartlib.go

package dartlib

function Hello() string { return "hello!" }

This can be imported as the following: "github.com/your_github_username/dartlib"


Program

Or, you can store the package within your program directory. In this case the directory structure should look something like the following:

dart (you_program_name)
|
 dart.go
 dartlib (package)
 |
  dartlib.go

In this case, the library can be imported as the following: "github.com/your_github_username/dart/dartlib"


dart.go

package main

import (
 "github.com/your_github_username/dart/dartlib"
 "fmt"
)

helloString := dartlib.Hello()
fmt.Println(helloString)

go build . at directory root produces the dart executable.

$./dart hello!

For more examples and further explanation see the docs

jonroethke
  • 1,152
  • 2
  • 8
  • 16
4

As Wombologist mentioned, you can split different files under the same package without problem, assuming they share the same package definition. The only exception for different package definitions under the same directory is for tests, where the recommended way of defining the package is adding _test on it (e.g. package main_test or package dart_test).

Not sure if it's your case here or if you are just experimenting with the language, but I would add that Go projects are easier to maintain when you group same domain code under packages and share them around, increasing the potential reusability.