Thursday, 1 October 2020

golang10 struct method

 package main

import "fmt"

type Student struct {
name   string
grades []int
age    int
}

func (s *Student) setAge(age int) {
s.age = age
}

func (s Student) getAverageGrade() float32 {
sum := 0
for _, v := range s.grades {
sum += v
}
return float32(sum) / float32(len(s.grades))
}

func main() {
s1 := Student{"Tim", []int{70, 90, 80, 85}, 19}
average := s1.getAverageGrade()
fmt.Println(average)
fmt.Println(s1)
s1.setAge(7)
fmt.Println(s1)
}

//cmd
C:\Users\bob\golang1>go run tutorial.go
81.25
{Tim [70 90 80 85] 19}
{Tim [70 90 80 85] 7}

reference:

No comments:

Post a Comment