GOLANG Básico - AULA 03: Interfaces, ponteiros e encapsulamento

Join the Penguin Dev Club for early access, exclusive videos, and exclusive mentorship.    / @lucas_badico   In lesson 3, we explored how Go solves the challenges of traditional object-oriented programming, abandoning rigid inheritance and classes in favor of a more pragmatic approach with interfaces, structs, and composition. We learned that interfaces are behavioral contracts that promote flexibility and polymorphism, while structs serve to store data and compose functionalities. We also discussed organizing projects with modules and packages, using go.mod to manage dependencies. We finished by building a modular calculator that demonstrates how interfaces and composition make code more reusable, scalable, and maintainable, aligned with Go's practical and efficient philosophy. 🚀 Timestamps: 00:00 - Introduction to lists in Go: arrays and slices 02:57 - Array limitations and introduction to slices 04:59 - The `append` function and how slices optimize capacity 07:44 - Difference between length and capacity in slices 10:07 - Dynamic growth of capacity in slices 14:19 - Using the `make` function to create slices with initial capacity 17:14 - Capacity encapsulation with `make` 18:35 - Sub-slices: concept and practical example 21:13 - Review: slices are abstractions over arrays 22:17 - Introduction to maps and the use of `make` 23:45 - Comparison between Structs and Maps: Key-Value 25:25 - Iteration with Maps and the `range` Function 27:23 - Interfaces: What They Are and Why They Are Useful 29:47 - Composition vs. Inheritance in Go 32:17 - Practical Implementation of Interfaces with Contracts 36:03 - Practical Example: Cars, Bicycles, and the `Vehicle` Interface 38:57 - Interface Location in Code: Close to Use 39:59 - Polymorphism and Composition with Interfaces 42:28 - Organizing Code with Interfaces and Composition 43:28 - Encapsulation and the Importance of Interfaces 45:05 - Interfaces as Abstract Types and References 46:34 - Understanding Modules and Package Organization 47:45 - Creating Packages and Organizing the Project with Modules 52:51 - Import and Export in Go: Public and Private 58:20 - Conventions Naming conventions for packages and files 01:02:33 - Encapsulation in Go: visibility rules 01:06:42 - Structs and encapsulation of public and private fields 01:10:22 - Code clarity with public and private visibility 01:12:12 - Importing external packages with `go get` 01:14:39 - Dependency management with `go mod tidy` 01:17:14 - Starting the calculator project in Go 01:19:39 - Implementing the `Operation` interface 01:20:44 - Creating basic operations: addition, subtraction, multiplication, and division 01:23:55 - Organizing code with maps and interfaces 01:27:28 - Reusing code with interfaces and methods 01:29:55 - Conclusion and audience questions 1. Challenges of OOP Classic: Rigid and coupled inheritance. Difficulties in code reuse and flexibility. Explosion of classes in complex systems. Use of design patterns to correct problems created by the OOP model. 2. Go as a Solution: Structs and Interfaces: Pragmatic representation of data and behaviors. Composition: Replacing inheritance, it promotes reuse without rigid coupling. Interfaces as Contracts: Define "what" to do, not "how". Modularity: Use of packages and modules (`go.mod`) for organization and dependency management. 3. Modules and Organization: How to initialize a module with `go mod init`. Structuring code with clear and independent packages. Automatic management of external dependencies directly from the code. 4. Practical Application – Modular Calculator: Building a calculator using: Interfaces: To represent mathematical operations. Composition: To create flexibility in the logic. Packages: To organize and separate responsibilities.