38 lines
1.3 KiB
Go
38 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math/rand"
|
|
"net/http"
|
|
)
|
|
|
|
type Question struct {
|
|
ID int `json:"id"`
|
|
Category string `json:"category"`
|
|
Question string `json:"question"`
|
|
Answer1 string `json:"answer1"`
|
|
Answer2 string `json:"answer2"`
|
|
Answer3 string `json:"answer3"`
|
|
Answer4 string `json:"answer4"`
|
|
CorrectAnswer int `json:"-"`
|
|
}
|
|
|
|
// let's declare a global Questions array
|
|
// that we can then populate in our main function
|
|
// to simulate a database
|
|
var Questions []Question
|
|
|
|
func initializeQuestions() {
|
|
Questions = []Question{
|
|
Question{ID: 0, Category: "Muzika", Question: "Koji kompozitor je stariji od Mozarta?", Answer1: "Haydn", Answer2: "Rizo Ruža", Answer3: "Wagner", Answer4: "Vatroslav Lisinski", CorrectAnswer: 1},
|
|
Question{ID: 1, Category: "Matematika", Question: "Beskonačno minus beskonačno - koliki je rezultat ?", Answer1: "0", Answer2: "Zavisi", Answer3: "2 beskonačna", Answer4: "Nedefinisano u matematici"},
|
|
Question{ID: 2, Category: "Književnost", Question: "Ko je napisao djelo 'Idiot'?", Answer1: "Dostojevski", Answer2: "Decembarski", Answer3: "Lenjin", Answer4: "Branko Čopić"},
|
|
}
|
|
}
|
|
|
|
func randomQuestionHandler(w http.ResponseWriter, r *http.Request) {
|
|
randomIndex := rand.Intn(len(Questions))
|
|
question := Questions[randomIndex]
|
|
json.NewEncoder(w).Encode(question)
|
|
}
|