I'm new in golang i want to use it to make a rest API. I'm blocking on this problem. I don't understand it because the function CreateEffect runs good.
My problem is in the function "getEffects" at line: "db.Find(&effects)"
This is my code:
var db *gorm.DB
var err error
func getEffects (w http.ResponseWriter, r *http.Request) {
var effects []Effects
db.Find(&effects)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&effects)
}
func main () {
db, err := gorm.Open("sqlite3", "gorm.db")
defer db.Close()
//Initialisation des routes
r:= mux.NewRouter()
r.HandleFunc("/effects", getEffects).Methods("GET")
//Start serveur port 5080
log.Fatal(http.ListenAndServe(":5080",r))
}
This is the runtime error:
2019/09/28 22:46:54 http: panic serving 127.0.0.1:36488: runtime error: invalid memory address or nil pointer dereference
goroutine 27 [running]:
The full code is here: https://pastebin.com/sbzhK8YV
Thanks for help