lab-development-imb/web/08/labor/anleitung_extlib.md

26 lines
996 B
Markdown
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Abhängigkeitsverwaltung mit `go.mod`
## Hintergrund
Externe Pakete, die im Code importiert werden, werden über eine `go.mod`Datei verwaltet. Diese Datei definiert das Modul und listet alle Abhängigkeiten auf. Sie gehört fest zum Projekt und wird im QuellcodeRepository mitgeführt.
## Anweisungen
Annahme: Go-Programm ist bereits vorhanden und die Imports im Code sind gesetzt. Außerdem befinden Sie sich in dem Ordner mit dem Go-Programm.
1. **Modul initialisieren (falls noch nicht geschehen)**
```bash
go mod init example.com/myapp
```
Der Modulpfad entspricht in der Praxis meist der RepositoryAdresse, z.B. `github.com/mymodule`. Für einfache Beispiele genügt ein Platzhalter wie `example.com/myapp`
2. **Abhängigkeiten auflösen und aufräumen**
```bash
go mod tidy
```
Dadurch werden alle im Code verwendeten Imports heruntergeladen und ins `go.mod` eingetragen. Nicht mehr benötigte Pakete werden entfernt.
3. **Programm starten**
```bash
go run .
```