package main import ( "database/sql" "flag" "fmt" "log" "encoding/json" _ "github.com/go-sql-driver/mysql" ) var ( dbURL = flag.String("db", "julian:hi@/test", "The Database") ) func main() { flag.Parse() db, err := sql.Open("mysql", *dbURL) if err != nil { log.Fatal(err) } if err = readStudiengaenge(db); err != nil { log.Fatal("readStudiengaenge:", err) } result, err := db.Query("SELECT Vorname, Nachname, Matrnr, Studiengang FROM Teilnehmer") if err != nil { log.Fatal("error fetching students:", err) } var ( stud Student ID int ) for result.Next() { if err = result.Scan(&stud.Vorname, &stud.Nachname, &stud.Matrnr, &ID); err != nil { log.Println("could not retrieve Student:", err) continue } name, ok := studiengaenge[ID] if !ok { log.Println("illegal Studiengang:", ID) continue } stud.Studiengang = name out, err := json.Marshal(stud) if err != nil { log.Fatal("could not marshal to json:", err) } fmt.Println(string(out)) } }