Organising my personal go code

Hi there,

I need a little eplanation on how to organise and write my gocode. Not in the aspect of setting my path and all, but while creating my own project.

For instance, I have a project folder called “firstapp” and I have these go files in it, “main.go”, “dbase.go”, “secure.go”.

I understand that my main.go file should have a “package main” declaration.
Question is:

---- What name package should I declare at the top of my dbase.go and secure.go files.

  • I know I should have my public funcs starting with Capslock.

Do I need to refer to each file with an import statement?


All files in the same directory belong to the same package, main in the case of an executable. You only need to import packages, not files. The How to Write Go Code article should clarify it.


Okay, thanks Jakob.

So, if I have three files in my project folder.

  1. main.go (has an header package )
  2. database.go (what package header will be declared here).
  3. stuffs.go (and what package header will be declared here)

Do I have to add package declaration (like I did in main) to every go file?


Ok, so for stuffs.go, that is not main, what should the pacjage header be?

A Go “package” is a directory of files. All files in a given directory belong to the same package. If you are building a command line tool called firstApp then every file in that directory should start with package main. If you are building a package in a directory foo/bar then every file in there should start with package bar. This is actually explained rather well in the article I linked. :wink:

Yes, and with the exceptions of test files (_test.go), every file in a package must have the same package declaration.

Oh!! Thanks for answering my questions.

So you mean all .go files in my firstApp directory should have package main declared at the top, even if I’m only referencing the functions in there (and not running them individually.)


1 Like

Ok, cool. So every file in my firstApp dir should have package main

From the link that @calmh provided above

Package names
The first statement in a Go source file must be
package name
where name is the package’s default name for imports. (All files in a package must use the same name.)

1 Like

You should just experiment, you’ll learn more if you answer your own questions. The worst that will happen is a compile error.

Thanks guys for the clarification. I’ll run my stuff to see how this works. :hamburger: for you Jakob and :rice: For Dave :grinning:

And a little heartbreak. I really hate that. lol

I recognize the problems OP had with files and packages. It’s the same I had when I started to work with Go. I like Go, and many concepts are easy, but some are hard and unexplained due to unclear documentation and books. For example, I’m able to create well written Go programs, but I never use interfaces. No book or documentation text has ever been able to make clear to me why and when to use them. And that frustrates when finding a solution for a simple problem like reading lines from a text file. For that you need to know that *os.File and io.Writer are compatible, and that that might have something to do with interfaces.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.