我已經(jīng)從數(shù)據(jù)庫中獲取了所有數(shù)據(jù),然后數(shù)據(jù)處于循環(huán)狀態(tài)。除了發(fā)生的循環(huán)之外,我想插入其他數(shù)據(jù),例如會話和標(biāo)題。然后我把它放入模板中。我猜測使用struct或slice,但學(xué)習(xí)后找不到解決方案。type Sekolah struct { Id int Nama string Alamat string Telp string }我有一個(gè)像上面這樣的結(jié)構(gòu),然后我創(chuàng)建了句柄函數(shù)http.HandleFunc("/sekolah", func(w http.ResponseWriter, r *http.Request) {//THIS IS THE DATA I WANT TO SERVE IN TEMPLATEvar title := "Some Title"var session := "MySession" db, errdb := sql.Open("postgres", koneksi) if errdb != nil { fmt.Println(errdb) } rows, err := db.Query("SELECT id, nama, alamat, telp FROM public.m_sekolah") if err != nil { fmt.Println(err) http.Error(w, "there was an error", http.StatusInternalServerError) return } var id int var nama string var alamat string var telp string var sk []Sekolah for rows.Next(){ err = rows.Scan(&id, &nama, &alamat, &telp) if err != nil { fmt.Println(err) http.Error(w, "There was an error", http.StatusInternalServerError) return } sk = append(sk, Sekolah{Id: id, Nama: nama, Alamat: alamat, Telp: telp}) } err = tmpl.ExecuteTemplate(w, "data_sekolah", sk) if err != nil { fmt.Println(err) }})我想將標(biāo)題和會話數(shù)據(jù)輸入到SK數(shù)據(jù)中但不參與循環(huán),然后我想將其粘貼到模板中。誰能幫我?
我想獲取數(shù)據(jù)會話和標(biāo)題,但從數(shù)據(jù)庫檢索數(shù)據(jù)時(shí)它位于循環(huán)之外
寶慕林4294392
2023-06-26 17:40:05