🏛️ Cap 10: Estruturas (Structs), Receiver Functions (Métodos) e Composição (Embedding) - Exemplos Práticos
Código executável completo demonstrando os conceitos abordados em Go.
🗺️ Fluxo de Execução
flowchart TD
COMPOSICAO["Composição / Embedding em Go"]
COMPOSICAO --> BASE["type Entidade struct { ID string }"]
COMPOSICAO --> USER["type Usuario struct {
Entidade // Campo anônimo embutido
Nome string
}"]
style COMPOSICAO fill:#e1f5fe,stroke:#03a9f4,stroke-width:2px
style USER fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
📄 Código de Demonstração (structs.go)
package main
import "fmt"
type Retangulo struct {
\Largura, Altura float64
}
// Método com Pointer Receiver
func (r *Retangulo) Area() float64 {
\return r.Largura * r.Altura
}
func main() {
\ret := Retangulo{Largura: 10, Altura: 5}
\fmt.Printf("Área do retângulo: %.2f\n", ret.Area())
}
🚀 Saída Esperada no Terminal
$ go run structs.go
Área do retângulo: 50.00
🧭 Navegação Rápida
| 📖 Teoria | 📊 Slides | 🧠 Quiz | 💻 Exemplos | 🧩 Exercícios | | :— | :— | :— | :— | :— | | Ler Tópico | Ver Slides | Fazer Quiz | Ver Código | Praticar |