I am trying to convert an int into a string. To do this, I need (int, error = strconv.Atoi(string)
But for some reason the error will become an unresolved reference.
If I change =
to :=
, like this (klantnummerInt, err := strconv.Atoi(klantnummerString)
), then I do not get the error. But then I run into another problem.
Because after the for loop, I need the int(klantnummerInt)
again. But I won't be able to get it, because it has been defined within the for loop and it is not accessible outside of the for loop.
I also tried to put the code into a switch statement, but if I do that it will give me the following error:
panic: sql: expected 0 arguments, got 1
func main() {
scanner := bufio.NewScanner(os.Stdin)
var klant Klant
var klantnummerString string
var klantnummerInt int
var keuze string
for {
fmt.Println("voer het klantnummer in")
fmt.Println("")
scanner.Scan()
klantnummerString = scanner.Text()
klantnummerInt, err = strconv.Atoi(klantnummerString)
if err != nil {
fmt.Println("Geen geldig Klantnummer ingevoerd!")
fmt.Println()
} else {
klant = klantnummerControleren(klantnummerInt)
if klant.Klantnummer == 0 {
fmt.Println()
fmt.Println("Geen klant met klantnummer", klantnummerInt, " aanwezig")
fmt.Println()
} else if klant.Klantnummer == klantnummerInt {
fmt.Println("Wilt u een bestelling aanmaken voor deze klant?")
fmt.Println(klant.Klantnummer, klant.Voornaam, klant.Achternaam)
fmt.Println("")
fmt.Println("[JA]")
fmt.Println("[NEE]")
fmt.Println("")
scanner.Scan()
keuze = strings.ToUpper(scanner.Text())
if keuze == "" {
fmt.Println("Maak een keuze!")
continue
} else if keuze == "JA" {
fmt.Println("")
fmt.Println("Uw keuze is JA")
} else {
if keuze != "JA" {
fmt.Println("")
fmt.Println("Uw keuze is NEE")
continue
}
}
break
}
}
}
klant = klantGegevens(klantnummerInt)
fmt.Println(klant.Leeftijd)
// begin hier
}
func klantnummerControleren(nummer int) Klant {
db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/vitaintellectdb")
checkErr(err)
queryKlantnummer := "SELECT klantnummer, voornaam, naam FROM klant WHERE klantnummer = ?"
rows, err := db.Query(queryKlantnummer, nummer)
checkErr(err)
var klant Klant
for rows.Next() {
err := rows.Scan(&klant.Klantnummer, &klant.Voornaam, &klant.Achternaam)
checkErr(err)
}
rows.Close()
return klant
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
type Klant struct {
Klantnummer int
Voornaam string
Achternaam string
}
It would be nice if err will not be an unresolved reference anymore. This will fix the problem.
If var klantnummer will be used within the for loop then I will be able to access it within the func main. That would be awesome.
Aucun commentaire:
Enregistrer un commentaire