New to Golang : Passing Array of struct to function

5,721 views
Skip to first unread message

Abhinav Srivastava

unread,
Oct 27, 2013, 3:13:53 AM10/27/13
to golan...@googlegroups.com
Hi,

I am new to Golang and trying my hand on basic Student class to search for student for the corresponding roll number but it is giving many errors and I am not able to understand how to call this function exactly. Here is my complete code and also please guide me , where I can improve more (I know I have a lot to :P) and any particular place from where I can study golang in depth.

package main

import (
    "fmt"
)

type student struct {
    name string
    roll int
    marks [3]int
}

func (s student) totalMarks() (int) {
    total:=0
    for i,_:=range s.marks {
        total=total+s.marks[i]
    }
    return total
}
 func (s student) search(stud *[]student,roll int) (student) {
    for i:=0;i<len(stud);i++ {
        if stud[i].roll==roll {
            fmt.Println("Found the student")
            return stud[i]
        }else{
            fmt.Println("No such student exists")
            return 
        }

    }

 }

func main() {
    var name string
    var roll int
    var marks [3]int
    var num int

    fmt.Print("Enter number of students you want to enter in record : ")
    fmt.Scan(&num)
    stud:= make([]student,num)

    for i:=0;i<num;i++ {

    fmt.Println("Enter the name : ")
    fmt.Scan(&name)
    fmt.Println("Enter roll number : ")
    fmt.Scan(&roll)
    fmt.Println("enter marks of 3 subjects")
    for i:=0;i<3;i++ {
        fmt.Scan(&marks[i])
    }
    s:=student{name,roll,marks}
    avg:=s.totalMarks()/3
    fmt.Println("the total is ",s.totalMarks())
    fmt.Println("average is ",avg)
    stud[i]=s;

 }


 }

Any comment or suggestion appreciated.

Thanks & Regards

Abhinav

Tamás Gulácsi

unread,
Oct 27, 2013, 5:15:51 AM10/27/13
to golan...@googlegroups.com
Hi,

1. use play.golang.org
2. Go does not have classes,
3. slices are already references, no need to use pointers for them (no *[]student)
4. what does student.search do? Why is it a method of student if you don't use the "s" student?

http://play.golang.org/p/Zhu3ohKwV8

Gerard

unread,
Oct 27, 2013, 7:03:36 AM10/27/13
to golan...@googlegroups.com
A little code cleanup http://play.golang.org/p/rwstdPSwNb

1. line 13: changed return. Now it's named. And because Go also zeros the variable, there is no need to make it zero first.
2. line 20: remark: if you want this modifyable, return a pointer to student.
3. line 37 to 49: There is no need for extra variables when you already have a slice of the student struct. I also used range in the for loops.

OpenNota

unread,
Oct 27, 2013, 9:49:20 AM10/27/13
to golan...@googlegroups.com

3. slices are already references, no need to use pointers for them (no *[]student)


It's not true. See http://blog.golang.org/slices "Pointers to slices: Method receivers"

Abhinav Srivastava

unread,
Oct 27, 2013, 2:21:16 PM10/27/13
to golan...@googlegroups.com
thanks a lot everyone..for your help and guiding me..

Only problem is since search function in my file is not bounded to student class then how am i suppose to call it?
my code shows error for the same.

Also , if anyone can provide me some link from where to study golang in depth.

Thanks again everyone :)

chris dollin

unread,
Oct 27, 2013, 2:38:23 PM10/27/13
to Abhinav Srivastava, golang-nuts
On 27 October 2013 18:21, Abhinav Srivastava <abhi1988s...@gmail.com> wrote:
thanks a lot everyone..for your help and guiding me..

Only problem is since search function in my file is not bounded to student class then how am i suppose to call it?

Remove the `( s student)` from the func definition. (Search doesn't
seem to me to be a responsibility of students.)
 
Also , if anyone can provide me some link from where to study golang in depth.

Read the language spec and tutorial and Effective Go.
Read godoc and code from the standard libraries.

--
Chris "allusive" Dollin

Abhinav Srivastava

unread,
Oct 27, 2013, 3:05:14 PM10/27/13
to golan...@googlegroups.com, Abhinav Srivastava, ehog....@googlemail.com
thanks chris..my bad..totally..its working now. :)
Reply all
Reply to author
Forward
0 new messages