Functions and Multiple Files
Why Functions?
Without functions, all code would be placed inside main().
Example:
func main() {
// hundreds of lines of code
}As a program grows, this becomes difficult to read and maintain.
Functions allow code to be divided into small, reusable units.
Defining a Function
func sayHello() {
fmt.Println("Hello")
}Calling the function:
sayHello()Output:
HelloFunction Anatomy
func sayHello() {
fmt.Println("Hello")
}Components:
funcKeyword used to define a function.
sayHelloFunction name.
()Parameter list.
{}Function body.
Function Parameters
Functions can receive input values.
func greet(name string) {
fmt.Println("Hello", name)
}Usage:
greet("Alice")
greet("Bob")Output:
Hello Alice
Hello BobParameter Syntax
Go places the type after the variable name.
func greet(name string)Compared to C++:
void greet(string name)This style is used consistently throughout Go.
Return Values
Functions can return data.
func add(a, b int) int {
return a + b
}Usage:
result := add(3, 5)
fmt.Println(result)Output:
8Multiple Parameters of Same Type
Instead of:
func add(a int, b int) intGo allows:
func add(a, b int) intThis is the preferred style.
Example: Square Function
func square(x int) int {
return x * x
}Usage:
fmt.Println(square(5))Output:
25Multiple Source Files
A package can be split across multiple files.
Directory:
main.go
helper.gomain.go
package main
import "fmt"
func main() {
fmt.Println(message())
}helper.go
package main
func message() string {
return "Hello from another file"
}Output:
Hello from another fileWhy Does This Work?
Both files belong to the same package:
package mainDuring compilation, Go combines all files belonging to the same package.
Conceptually:
Package main
├── main.go
└── helper.goFunctions in one file can be called directly from another file in the same package.
No import is required.
Package vs Module
A common source of confusion.
Structure:
Module
└── Package
├── File
├── File
└── FileExample:
myproject/
go.mod
main.go
helper.gogo.mod defines the module.
Files beginning with:
package mainbelong to the same package.
Go builds all files in the package together.
Concepts Learned
Functions
func greet() {
}Reusable blocks of code.
Parameters
func greet(name string)Allow functions to receive data.
Return Values
func greet() stringAllow functions to return data.
Multiple Parameters
func add(a, b int) intShared type declaration.
Multiple Files
A package may span several source files.
Packages
package mainFiles in the same package can access each other’s exported and unexported functions.
Session Summary
Topics covered:
- Function definition
- Function calls
- Parameters
- Return values
- Multiple parameters
- Splitting code across files
- Package organization
- Relationship between modules, packages, and files
Practical skills gained:
- Creating reusable functions
- Returning values from functions
- Organizing code across multiple files
- Understanding how Go builds packages
Key Takeaway
Functions are Go’s primary way to organize behavior.
Packages are Go’s primary way to organize code.
A typical Go program grows by:
Variables
↓
Functions
↓
Multiple Files
↓
Packages
↓
StructsThe next major topic is Structs, which provide a way to group related data together.
Go Learning - Session 4