FAQ
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

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Tamás Gulácsi at Oct 27, 2013 at 9:15 am
    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

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Oct 27, 2013 at 11:03 am
    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.

    On Sunday, October 27, 2013 10:15:51 AM UTC+1, Tamás Gulácsi wrote:

    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
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • OpenNota at Oct 27, 2013 at 1:49 pm

    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"

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Abhinav Srivastava at Oct 27, 2013 at 6:21 pm
    thanks a lot everyone..for your help and guiding me..
    Here is the code : http://play.golang.org/p/pjdr3QSBV7

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

    On Sunday, October 27, 2013 2:15:51 AM UTC-7, Tamás Gulácsi wrote:

    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
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Oct 27, 2013 at 6:38 pm

    On 27 October 2013 18:21, Abhinav Srivastava wrote:

    thanks a lot everyone..for your help and guiding me..
    Here is the code : http://play.golang.org/p/pjdr3QSBV7

    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

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Abhinav Srivastava at Oct 27, 2013 at 7:05 pm
    thanks chris..my bad..totally..its working now. :)
    On Sunday, October 27, 2013 11:38:23 AM UTC-7, chris dollin wrote:

    On 27 October 2013 18:21, Abhinav Srivastava <abhi1988s...@gmail.com<javascript:>
    wrote:
    thanks a lot everyone..for your help and guiding me..
    Here is the code : http://play.golang.org/p/pjdr3QSBV7

    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
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 27, '13 at 7:13a
activeOct 27, '13 at 7:05p
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase