⚡ Cap 08: Funções, Retornos Múltiplos, Variádicas, Funções Anônimas e Closures - Exemplos Práticos
Código executável completo demonstrando os conceitos abordados em Go.
🗺️ Fluxo de Execução
flowchart LR
CALL["dividir(10, 2)"] --> FN["func dividir(a, b int) (int, error)"]
FN --> RET["Retorna (5, nil) como múltiplos valores diretamente"]
style CALL fill:#e1f5fe,stroke:#03a9f4,stroke-width:1px
style FN fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
style RET fill:#fff3e0,stroke:#ff9800,stroke-width:1px
📄 Código de Demonstração (funcoes.go)
package main
import (
"errors"
"fmt"
)
func dividir(a, b float64) (float64, error) {
\if b == 0 {
\return 0, errors.New("divisão por zero proibida")
}
\return a / b, nil
}
func main() {
\res, err := dividir(10, 2)
\if err != nil {
\fmt.Println("Erro:", err)
\return
}
\fmt.Printf("Resultado: %.2f\n", res)
}
🚀 Saída Esperada no Terminal
$ go run funcoes.go
Resultado: 5.00
🧭 Navegação Rápida
| 📖 Teoria | 📊 Slides | 🧠 Quiz | 💻 Exemplos | 🧩 Exercícios | | :— | :— | :— | :— | :— | | Ler Tópico | Ver Slides | Fazer Quiz | Ver Código | Praticar |