- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program That Creates a Channel of Type String And a Goroutine That Sends a Message to The Channel Every 2 Seconds
In Go, channels are a capable highlight for concurrent programming, empowering communication and synchronization between goroutines. In this article, we are going to investigate how to form a channel of sort string in Go and utilize a goroutine to send messages to the channel at normal intervals of 2 seconds. We are going to give a step-by-step exhibit of the program, displaying the utilization of channels and goroutines.
Syntax
time.NewTicker(time.Second)
The Syntax time.NewTicker(time.Second) is used to create a new Ticker value from the time package in Go.
time.Sleep(duration)
The Syntax time.Sleep(duration) is used to pause the execution of a program or goroutine for a specified duration.
Algorithm
Step 1 − Import the specified packages −
Step 2 − Import the fmt bundle for printing messages to the console.
Step 3 − Import the time package for overseeing time-related operations.
Step 4 − Use the make work to form a channel of sort string: ch := make(chan string). Create a goroutine −
Step 5 − Use the time.Sleep work to delay execution for 2 seconds between each message.
Step 6 − Use the channel administrator as main.go.
Step 7 − Open a terminal or command incite, explore to the file's registry, and execute the program utilizing the go run command: go run main.go.
Example 1
In this case, we make the channel ch, begin the goroutine to send messages to the channel, and after that persistently receive and print the messages within the fundamental goroutine utilizing an unbounded for loop.
package main import ( "fmt" "time" ) func main() { ticker := time.NewTicker(time.Second) tickerChan := ticker.C done := make(chan bool) go func() { for { select { case <-tickerChan: fmt.Println("Ticker ticked!") case <-done: return } } }() time.Sleep(5 * time.Second) done <- true ticker.Stop() fmt.Println("Ticker stopped.") }
Output
Ticker ticked! Ticker ticked! Ticker ticked! Ticker ticked! Ticker ticked! Ticker stopped.
Example 2
In this case, we make the channel ch, start the goroutine to send messages to the channel, and after that ceaselessly get and print the messages within the fundamental goroutine utilizing a boundless circle.
package main import ( "fmt" "time" ) func main() { go performTask() time.Sleep(5 * time.Second) fmt.Println("Main goroutine exited.") } func performTask() { fmt.Println("Performing task...") time.Sleep(2 * time.Second) fmt.Println("Task completed.") }
Output
Performing task... Task completed. Main goroutine exited.
Conclusion
In this article, we investigated how to form a Go program that utilizes channels and goroutines to attain concurrency. By making a channel of sort string and a goroutine that sends a message to the channel every 2 seconds, we illustrated an essential case of communication and synchronization between goroutines. Channels empower secure and effective information sharing among goroutines, permitting you to construct concurrent applications in Go.