How to stop a go code after running for 3 hours

code that stop in 3 hours

create a go routine that gets the timestamp at startup and compare that to the current time every once in a while. if the timestamp differs 3 hours exit your code.

but there are many more ways to do so.

or just like that:

package main

import (
	"fmt"
	"time"
	"os"
)

func endCode() {
	time.Sleep(3 * time.Hour)
	os.Exit(1)
}

func main() {
	go endCode()
	for {
		fmt.Println("Hello, playground")
		time.Sleep(1 * time.Minute)
	}
}
1 Like

Create a clock application written in go that will print the following values at the following intervals to stdout:

  • “dog” every second

  • “cat” every minute

  • “house” every hour

It should run for three hours and then exit.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.