How do I name packages and files idiomatically in Go?

In Go, naming packages and files is crucial for the readability and maintainability of your code. Here are some idiomatic guidelines:

  • Use short, concise names: Package names should be shorter than the file name and indicate the primary functionality. For example, instead of naming a package "mathOperations", a better name would be "math".
  • Use lower case letters: Package names should be in all lower case letters, with no underscores or mixed caps.
  • Use descriptive names: While being concise, package names should still convey their purpose clearly.
  • Avoid using the same name in different directories: Each package must have a unique name within the broader module, hence avoid duplication.
  • Group related functionality: If certain functionality is closely related, consider grouping them together in the same package.

Here is an example of an idiomatic package naming convention:

// Example package structure mymodule/ └── math/ ├── add.go ├── subtract.go └── multiply.go

go golang packages naming convention idiomatic go