New course! Every coder should learn Generative AI!
Try a free lesson0
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.
4 Answers
+ 4
Kindly show your attempt
+ 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()
}
}
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)
}
0
Expected out is (ABC 0)