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)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
