Search data in array of struct

I would like to search data in array of struct.
Var vcustomer :=[]customers
Example : select * from customers stored in vcustomer

Var vorder := []orders
Select * from orders stored in borders

For _,cust range vcustomers{
Find orders,}

How to find customers orders from order array.

One way we can compare.

Is there any other and fast way like indexing and searching

1 Like

If you are searching many times, create a fast data structure, such as a map[customer][]order. If you are doing it just once, then search linearly through the orders slice.

Please give me example

Maybe if you provide some concrete code, I’ll add a few lines to it.

type SudentsMarks struct {

	Student_code int `db:"student_code"`
	Subjectname string `db:"subjectname"`
	Marks   int    `db:"marks"`

type Students struct {
Student_code int db:"student_code"
Batch_division string db:"batch_division"
Rollno int db:"rollno"
Name string db:"fullname"
Marks []SudentsMarks
var err error
var _students = []Students{}
var _studentsMarks = []SudentsMarks{}

studentquery := select * from students
marksquery := select * from marks

err = Mydb.Select(&studentquery, studentquery)
if err != nil {
return err

err = Mydb.Select(&_studentsMarks, marksquery)
if err != nil {
return err

now we want to embed marks inside students , because in my student structure marks is array.
we are using linear search

for nstudentloop, student := range _students {

		for _, marksloop := range _studentsMarks {
			if marksloop.student_code == student.student_code {

				_marks := SudentsMarks{}
				_marks.Marks = marksloop.Marks
				_marks.student_code = marksloop.student_code
				_marks.subjectname = marksloop.subjectname

				_students[nstudentloop].Marks = append(_students[nstudentloop].Marks, _marks)
			} else {


This code is working but according to me there may be better way to solve this issues.
The marks loop run multiple time .

If you create a mapping from student code to Students, then you don’t have to do an O(n^2) nested loop. The following is O(n). I don’t know why you create a new SudentsMarks in the inner loop instead of reusing the one in the slice.

studentsByCode := make(map[int]*Students)
for i := range _students {
  studentsByCode[s[i].Student_code] = &s[i]

for _, m := range _studentsMarks {
  s,ok := studentsByCode[m.student_code]
	if ok {
		s.Marks = append(s.Marks, m)
	} else {
		panic("missing student for marks")

My requirement is
i want json in this format

Student_code : 1
Batch_division : “A”
Rollno : 10
Name : “Mr james”
marks : [ {"Student_code : 1 , Subjectname : “maths”,Marks : 60},
{"Student_code : 1 , Subjectname : “eng”,Marks : 70}]

I am executing 2 separate query . I would like to link both data by using student_code .

You can add “json:…” to the tags in your structs, but you may not even need that. Then you do

That what’s the code to build the studentsByCode map does. What is missing?

In your approach . I have to transfer my struct data into map .

using following code according to you.
studentsByCode := make(map[int]*Students)
for i := range _students {
studentsByCode[s[i].Student_code] = &s[i]

The map holds a pointer to each struct, so there’s minimal extra memory required and no copying. In terms of space-time tradeoff, there can be a lot of time saved for just a little bit of extra space.

Thank so much it is really fast and perfect logic.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.