new to programming / even newer to go. having trouble with a small go program - will not compile with undefined variable errors. the code:
package main
import (
"fmt"
"io"
"os"
)
const file = "readfile.txt"
var s string
func lookup(string) (string, string, string) {
artist := s
album := s
year := s
return artist, album, year
}
func enterdisk() (string, string, string) {
var artist string
var album string
var year string
println("enter artist:")
fmt.Scanf("%s", &artist)
println("enter album:")
fmt.Scanf("%s", &album)
println("enter year:")
fmt.Scanf("%s", &year)
return artist, album, year
}
func main() {
println("enter UPC or [manual] to enter information manually:")
fmt.Scanf("%s", &s)
s := s
switch s {
case "manual
": artist, album, year := enterdisk()
default: artist, album, year := lookup(s)
}
f,_ := os.OpenFile(file, os.O_APPEND|os.O_RDWR, 0666)
io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "
"))
f.Close()
println("wrote data to file")
}
and the errors:
catalog.go:49: undefined: artist
catalog.go:49: undefined: album
catalog.go:49: undefined: year
however, those variables will not be defined until the code runs. additionally, the "lookup" function is not yet written, which it just returns what it is passed. i know the lookup and enterdisk functions work on their own, but i am trying to test the switch statement.
i have tried declaring the variables in main, however i get this error:
catalog.go:49: artist declared and not used
catalog.go:49: album declared and not used
catalog.go:49: year declared and not used
p.s. i have read http://tip.goneat.org/doc/go_faq.html#unused_variables_and_imports , and i agree that if this is only semantics, i still want to fix it. i just don't know how!