Commits

Cédric Bonhomme committed e16c67b

Added file_name parameter.

Comments (0)

Files changed (1)

 )
 
 type Quotes struct {
-    Quote    []Quote    `xml:"quote"`
+    Quotes    []Quote    `xml:"quote"`
 }
 
 // Defintion of the structure of a quote.
 }
 
 // Read a file and return a string.
-func read() string {
-    inputFile := "./quotes.xml"
+func read(file_name string) string {
+    inputFile := file_name
     buf, err := ioutil.ReadFile(inputFile)
     if err != nil {
         fmt.Fprintf(os.Stderr, "File Error: %s\n", err)
 }
 
 // Return a random quote.
-func getQuote() {
-    quotes_str := read()
+func getQuote(file_name string) {
+    quotes_str := read(file_name)
     var quotes Quotes
 
     err := xml.Unmarshal([]byte(quotes_str), &quotes)
     checkError(err)
 
     // Get the number of quotes
-    nb_quotes := len(quotes.Quote)
+    nb_quotes := len(quotes.Quotes)
     rand.Seed(time.Now().UnixNano())
     quote := rand.Intn(nb_quotes)
 
     // now use the person structure e.g.
-    fmt.Println("Citation: \"" + quotes.Quote[quote].Citation + "\"")
-    fmt.Println("Author: \"" + quotes.Quote[quote].Author + "\"")
-    fmt.Println("Year: \"" + quotes.Quote[quote].Year + "\"")
+    fmt.Println("Citation: \"" + quotes.Quotes[quote].Citation + "\"")
+    fmt.Println("Author: \"" + quotes.Quotes[quote].Author + "\"")
+    fmt.Println("Year: \"" + quotes.Quotes[quote].Year + "\"")
 }
 
 
 func main() {
-    getQuote()
+    getQuote("./quotes.xml")
 }