Golang / GoLang Basics Interview Questions
What are packages in Go and what is special about the 'main' package?
Every Go source file starts with a package declaration. Packages are Go's unit of code organisation, encapsulation, and compilation. A package groups related types, functions, constants, and variables.
The main package is unique: it defines an executable program. The main() function within it is the program's entry point. Any package not named main is a library package — importable by others but not directly runnable.
Exported vs unexported: identifiers starting with an uppercase letter are exported (public). Lowercase identifiers are unexported (package-private). This is Go's entire visibility system — no public, private, or protected keywords.
// main.go â executable program package main import ( "fmt" "myproject/mathutil" // importing a library package ) func main() { result := mathutil.Add(3, 4) // Add is exported (uppercase) fmt.Println(result) // 7 } // mathutil/math.go â library package package mathutil func Add(a, b int) int { return a + b } // exported func helper(x int) int { return x * 2 } // unexported (private)
More Related questions...