📦 Class 16 — Package Scope
🎥 Video Title: Package scope
🧪 Code Written for This Class
add.go
package main
import "fmt"
func add(n1, n2 int) {
res := n1 + n2
fmt.Println(res)
}
main.go
package main
var (
a = 20
b = 30
)
func main() {
add(4,7)
}
mathlib/math.go
package mathlib
import "fmt"
func Add(x int, y int) {
z := x + y
fmt.Println(z)
}
main.go (Modified)
package main
import (
"fmt"
"example.com/mathlib"
)
var (
a = 20
b = 30
)
func main() {
fmt.Println("Showing Custom Package")
mathlib.Add(4,7)
}
🔑 Key Concepts
-
Same Folder = Same Package All
.gofiles in the same directory should have the same package name (mainif you want to run them). -
Running Multiple Files You must include all necessary files when using
go run, like:go run main.go add.go -
Initializing a New Module Start with:
go mod init <module_name> -
Managing Dependencies Use:
go get <package_name> go mod tidy -
Package-Level Scope Rules Only exported identifiers (functions/variables that start with a capital letter) can be accessed from outside the package.
🧠 This class was all about understanding how Go handles packages, visibility, and modular code — crucial stuff for building real-world Go apps!