S
S
s_pyanov2016-11-17 06:51:41
go
s_pyanov, 2016-11-17 06:51:41

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 }}

then all 1000 lines are generated, although I have only 10 data, for example ...
I tried to use slices, but I have to declare the slice earlier and the size of the slice, respectively, too - this is not a way out (maybe I'm wrong here). Then the question arises, how can I check if the string is empty at the output stage of the html page? Or what is the best way to implement it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Volka, 2016-11-17
@s_pyanov

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)
}

Here there will be as much data as you place, and even if there are more than 1000 of them, everything will fit in this slice, it will just work more slowly.

A
Alexander Pavlyuk, 2016-11-17
@pav5000

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.

S
s_pyanov, 2016-11-18
@s_pyanov

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()	//Закрываем соединение с БД
}

it may not be very elegant, but it works ...
following the example of Volka Ladoshkin , when I update the page, the slice grows to the size of itself + a new entry ...
now, if you tell me how to use Volka Ladoshkin 's example correctly in my case, I will be grateful)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question