Go Workspace & Modules
Part of the Object Oriented Programming section of Coddy's GO journey — lesson 2 of 107.
A Go module is a collection of related Go files. The go.mod file at the root of your project declares the module name and Go version.
Initialize a module with the terminal command
go mod init myappThis creates a go.mod file
module myapp
go 1.21A simple project structure
myapp/
├── go.mod
├── main.go
└── helper.goAll files share the same package
// helper.go
package main
func Double(n int) int {
return n * 2
}// main.go
package main
import "fmt"
func main() {
result := Double(5)
fmt.Println(result) // Output: 10
}The go.mod file defines your project as a module. All .go files declaring the same package can access each other's functions directly. The module name in go.mod identifies your project.
Challenge
EasyComplete mathhelper.go within the same module and package:
Double— returns n * 2Triple— returns n * 3Square— returns n * n
Cheat sheet
A Go module is a collection of related Go files declared by a go.mod file at the project root.
Initialize a module:
go mod init myappThis creates a go.mod file:
module myapp
go 1.21All .go files in the same package can access each other's functions directly:
// helper.go
package main
func Double(n int) int {
return n * 2
}// main.go
package main
import "fmt"
func main() {
result := Double(5)
fmt.Println(result) // Output: 10
}Try it yourself
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
fmt.Printf("Double: %d\n", Double(n))
fmt.Printf("Triple: %d\n", Triple(n))
fmt.Printf("Square: %d\n", Square(n))
}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