- Package: Collection of Go files in the same directory
- Module: Collection of packages with
go.modfile go mod init: Initialize a new module- Import path:
modulename/packagenameformat - Public identifiers: Start with uppercase letter (exported)
- Private identifiers: Start with lowercase letter (unexported)
- Main package: Special package for executable programs
- Package declaration:
package nameat top of each .go file
- Package is a collection of Go files in the same directory.
- Module is a collection of packages.
- Program is a collection of main package and its dependencies.
- Process is a running instance of a program.
go mod init simplecalcThis will create a go.mod file in current directory. simplecalc is the name of the module.
simplecalc/ # This is the root directory of the module (where go mod init simplecalc was executed)
├── go.mod # Module definition file
├── main.go # Main package file (main.go)
└── mymath/ # Directory for mymath package
├── add.go # package mymath
├── subtract.go # package mymath
└── ... # (Other files in the mymath package)
package main
import (
"fmt"
"simplecalc/mymath" // simplecalc is the module name, mymath is the package name
)
func main() {
sum := mymath.Add(1, 2)
fmt.Println("1 + 2 =", sum)
}In Go, identifiers (names of variables, functions, types, etc.) are public if they start with a capital letter and private if they start with a lowercase letter.
package mymath
func Add(a, b int) int {
return a + b
}
func subtract(a, b int) int {
return a - b
}In the above code, Add is a public function and subtract is a private function.