๐Ÿ“ฆ 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

  1. Same Folder = Same Package All .go files in the same directory should have the same package name (main if you want to run them).

  2. Running Multiple Files You must include all necessary files when using go run, like:

    go run main.go add.go
    
  3. Initializing a New Module Start with:

    go mod init <module_name>
    
  4. Managing Dependencies Use:

    go get <package_name>
    go mod tidy
    
  5. 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!