Go practice problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Go practice problem

The given code declares a Timer struct, which is initialized in main(). You need to add a reset functionality to the program. The reset() function needs to take a pointer to the struct as its argument and reset the seconds value to 0.

23rd Jul 2021, 9:31 AM
Chinmay Anand
Chinmay Anand - avatar
4 Answers
+ 4
Kindly show your attempt
23rd Jul 2021, 10:19 AM
Aysha
Aysha - avatar
+ 4
Try it out in this way package main import "fmt" type Timer struct { id string value int } func (ptr *Timer) tick(){ //reference variable ptr.value++ // increment the timer fmt.Println(ptr.value) //print the current time value } func main() { var x int fmt.Scanln(&x) t := Timer{"timer1", 0} for i:=0;i<x;i++ { //using loop call the timer function t.tick() } }
23rd Jul 2021, 11:00 AM
Aysha
Aysha - avatar
0
package main import "fmt" type Timer struct { id string seconds int } func main() { var x int fmt.Scanln(&x) t :=&Timer{"ABC", x} func reset("ABC",0) fmt.Println(t) }
23rd Jul 2021, 10:31 AM
Chinmay Anand
Chinmay Anand - avatar
0
Expected out is (ABC 0)
23rd Jul 2021, 11:05 AM
Chinmay Anand
Chinmay Anand - avatar