3

OK, Go's major selling point is its ease of compilation and wonderful toolchain, but as a go newbie I'm really lost there and can't understand the documentation on that point.

I have a stack toy example within two files (one for the type definition and methods, called stack.go, one for the main program, called main.go), both are in my GOPATH/src/stacker directory.

  1. How should each file be named ? Does it have any importance at all ? Is there at least a convention ? A mandatory naming ?
  2. What should be the package name ? I understood they should use the same package name, but which one ? Is it stacker ?
  3. In main.go, how should I use the import directive to import stack.go ?

I have tried many combinations, none working until now.

Fabien
  • 12,486
  • 9
  • 44
  • 62

1 Answers1

9
  1. You can name the files however you like, just beware of special suffixes like _test and _<arch> (_darwin, _unix, etc.). Also note that files prefixed with . or _ won't be compiled into the package!
  2. It is recommended that you name the package like the folder the file is in, although it's possible (but confusing) to name a package differently in the declaration package mypkg
  3. If stack.go is in the same folder/package as main.go, you don't need to import. Everything delcared in stack.go is already available in main.go, because it is in the same package.

If stacker should compile into an executable, you should use package main.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • 4
    You should mention that, if `stacker` should compile into an executable instead of being an import package, it be should be `package main`. – ANisus Mar 08 '13 at 14:42
  • @ANisus and Erik, thanks for your answers. Works perfectly now and I'm starting to get the overall idea. – Fabien Mar 08 '13 at 14:46