External Files
Part of the Object Oriented Programming section of Coddy's GO journey — lesson 1 of 107.
In Go, you can split your code across multiple .go files within the same package. All files sharing the same package declaration can access each other's functions and types directly — no import needed.
// helper.go
package main
func Add(a, b int) int {
return a + b
}Use the function in another file of the same package
// main.go
package main
import "fmt"
func main() {
result := Add(3, 5)
fmt.Println(result) // Output: 8
}Both files declare package main, so Add is available in main.go without any import. This lets you organize code into logical files while keeping them in the same package.
Challenge
EasyCreate two functions in helper.go that are used by main.go:
Add— takes twointparameters and returns their sumSubtract— takes twointparameters and returns their difference
Cheat sheet
You can split your code across multiple .go files within the same package. All files sharing the same package declaration can access each other's functions and types directly without importing.
// helper.go
package main
func Add(a, b int) int {
return a + b
}// main.go
package main
import "fmt"
func main() {
result := Add(3, 5)
fmt.Println(result) // Output: 8
}Both files declare package main, so functions defined in one file are available in the other without any import statement.
Try it yourself
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Printf("Sum: %d\n", Add(a, b))
fmt.Printf("Difference: %d\n", Subtract(a, b))
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of Go OOP
External FilesGo Workspace & ModulesPackages & ImportsExported vs Unexported NamesIntroduction to OOP in GoStructs as ClassesDefining Methods on StructsPointer vs Value ReceiversStruct InitializationConstructor FunctionsRecap - Simple Calculator4Interfaces
Introduction to InterfacesImplicit ImplementationInterface as ContractEmpty Interface (any)Type AssertionType SwitchInterface CompositionStringer & Error InterfacesRecap - Shape Calculator7Encapsulation
Exported vs Unexported FieldsPackage-Level EncapsulationGetter & Setter MethodsInformation Hiding in GoRecap - Student Records10Generics (Go 1.18+)
Introduction to GenericsType ParametersType ConstraintsGeneric StructsGeneric Methods WorkaroundRecap - Generic Collection2Types & Structs Deep Dive
Basic & Composite TypesCustom Type DefinitionsStruct TagsAnonymous StructsNested StructsZero Values & DefaultsRecap - Contact Book5Composition Over Inheritance
Why Go Has No InheritanceStruct Embedding BasicsMethod PromotionEmbedding Multiple StructsEmbedding vs AggregationShadowing Embedded MethodsRecap - Employee Hierarchy8Error Handling & OOP
The error InterfaceCustom Error TypesError Wrapping (fmt.Errorf)Sentinel Errorserrors.Is() and errors.As()Panic, Defer, and RecoverRecap - File Parser3Pointers & Memory
Pointer Basics in GoPointers to StructsPass by Value vs ReferenceThe new() FunctionGarbage Collection in GoRecap - Linked List Builder6Polymorphism in Go
Polymorphism via InterfacesDuck Typing in GoInterface Satisfaction RulesPolymorphic CollectionsDependency InjectionRecap - Payment Processor9Concurrency & OOP
Goroutines BasicsChannels & CommunicationBuffered vs Unbuffered ChanSelect Statementsync.Mutex & sync.RWMutexsync.WaitGroupThread-Safe Struct DesignRecap - Worker Pool