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.