ADVERTISEMENT
ADVERTISEMENT

Golang program to print the received value from the ticker

Here, we are going to learn how to print the received value from the ticker in Golang (Go Language)?
Submitted by Nidhi, on April 28, 2021

Problem Solution:

Here, we will implement a ticker using time.NewTicker() function and print notified value on the console screen.

Program/Source Code:

The source code to print the received value from the ticker is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Golang program to print the received
// value from the ticker

package main

import "fmt"
import "time"

func main() {
	MyTicker := time.NewTicker(1 * time.Second)

	go func() {
		for {
			val := <-MyTicker.C
			fmt.Println(val)
		}
	}()

	time.Sleep(6 * time.Second)
	fmt.Println("Ticker finished")
}

Output:

2021-04-28 03:59:58.608391459 +0000 UTC m=+1.048522317
2021-04-28 03:59:59.608418517 +0000 UTC m=+2.048549380
2021-04-28 04:00:00.60841732 +0000 UTC m=+3.048548182
2021-04-28 04:00:01.608429595 +0000 UTC m=+4.048560451
2021-04-28 04:00:02.60843223 +0000 UTC m=+5.048563089
2021-04-28 04:00:03.608450903 +0000 UTC m=+6.048581811
Ticker finished

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the required packages to predefined functions.

In the main() function, we created a ticker MyTicker using time.NewTicker() function for 1 second, and printed the notified value on the console screen.

Golang Timers & Tickers Programs »



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.