๐ฆ 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
.go
files in the same directory should have the same package name (main
if 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!