Answer the question
In order to leave comments, you need to log in
How to display only completed rows from array on html page in go?
Good day to all.
There is an array with a length of 1000 lines, there is a database from which the lines of the array are filled. The rows in the database table are still less than the size of the array. If I output an array of 1000 rows through the construct:
{{ range . }}
...
<td class=" ">{{ .Pacientid }}</td>
<td class=" ">{{ .Tel}}</td>
{{ end }}
Answer the question
In order to leave comments, you need to log in
In your case, you need to declare a slice of zero length, and reserve memory for the intended size (the third parameter to make()):
patients := make([]Patient, 0, 1000)
//...
for row.Next() {
var patient Patient
row.Scan(&patient.Patientid, &patient.Tel)
patients = append(patients, patient)
}
The size of the slice does not have to be declared in advance, you can use the append function to add elements to the slice, the size of the slice is automatically increased by doing so.
Or you can put an if in your template to test for nullity.
Thanks to all!
While waiting for a response, I did this:
func getAllPacients(){ //Соединение с БД
var i int //Переменная для обхода масива
i = 0
db, err := sql.Open("mysql", username+":"+password+"@tcp("+server+":3306)/clinic") //Настраиваем соединение с БД
rows,err :=db.Query("SELECT * FROM Pacient") //Выборка всех пациентов
checkErr(err)//Проверяем отсутсвие ошибок
tmp := pacientStruct{}
Counter = conterQuey() // запрашиваем кол-во строк в таблице
fmt.Println("Counter =",Counter)
fmt.Println("=============================================================")
for rows.Next() { //получаем построчно информацию из запроса
//Присваиваем значение переменным получая их их последовательно из интерфейса
err = rows.Scan(&tmp.Id, &tmp.Pacientid, &tmp.FirstName, &tmp.LastName, &tmp.MiddleName, &tmp.Sex, &tmp.Birthday, &tmp.Tel, &tmp.Tel2, &tmp.Adress, &tmp.Email, &tmp.Whatsapp)
checkErr(err) //проверяем на ошибку
fmt.Print("RowScan = ")
if len(PacientResultSlice) < Counter {
PacientResultSlice = PacientResult[:Counter]
}
PacientResultSlice[i].Id=tmp.Id
PacientResultSlice[i].Pacientid=tmp.Pacientid
PacientResultSlice[i].FirstName=tmp.FirstName
PacientResultSlice[i].LastName=tmp.LastName
PacientResultSlice[i].MiddleName=tmp.MiddleName
PacientResultSlice[i].Sex=tmp.Sex
PacientResultSlice[i].Birthday=tmp.Birthday
PacientResultSlice[i].Tel=tmp.Tel
PacientResultSlice[i].Tel2=tmp.Tel2
PacientResultSlice[i].Adress=tmp.Adress
PacientResultSlice[i].Email=tmp.Email
PacientResultSlice[i].Whatsapp=tmp.Whatsapp
fmt.Print("PacientResultSlice[",i,"]= ",PacientResultSlice[i].Id)
fmt.Print(" Длина PacientResultSlice = ",len(PacientResultSlice))
fmt.Println(" | i = ", i, " ")
i++
}
db.Close() //Закрываем соединение с БД
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question