SoFunction
Updated on 2025-04-11

Detailed explanation of the implementation method of Timer without buffer in Go 1.23

In Go 1.23, the implementation of Timer is usually implemented through the types provided by the time package. Timer is a timer used to trigger an event after a specified time. Timer implementation does not rely on buffers, but is implemented through Go's scheduler and channel mechanism.

Basic implementation of Timer

The core of Timer is a structure that contains a channel to which the current time will be sent when the timer expires.

Here is a simple Timer implementation example:

package main

import (
	"fmt"
	"time"
)

func main() {
	// Create a Timer, set it to trigger after 2 seconds	timer := (2 * )

	// Wait for Timer to trigger	<-
	("Timer expired")

	// If you want to stop Timer, you can use the Stop() method	// ()
}

Implementation of no buffers

Timer implementation does not rely on buffers, but is implemented through Go's channel mechanism. is an unbuffered channel, and the current time will be sent to this channel when the timer expires. Since the channel is unbuffered, the sending operation blocks until a recipient is ready to receive the data.

Custom unbuffered Timer implementation

If you want to implement an unbuffered Timer yourself, you can use the function, which returns a channel, and when the specified time arrives, the channel will receive a time value.

package main

import (
	"fmt"
	"time"
)

func main() {
	// Create an unbuffered Timer using	timerCh := (2 * )

	// Wait for Timer to trigger	<-timerCh
	("Timer expired")
}

More complex Timer implementations

If you need more complex Timer implementations, such as Timer that can be reset or stopped, you can refer to the following code:

package main

import (
	"fmt"
	"time"
)

type MyTimer struct {
	duration 
	timer    *
	resetCh  chan 
	stopCh   chan struct{}
}

func NewMyTimer(duration ) *MyTimer {
	t := &MyTimer{
		duration: duration,
		resetCh:  make(chan ),
		stopCh:   make(chan struct{}),
	}
	 = (duration)
	go ()
	return t
}

func (t *MyTimer) run() {
	for {
		select {
		case <-:
			("Timer expired")
			return
		case newDuration := <-:
			if !() {
				<-
			}
			(newDuration)
		case <-:
			if !() {
				<-
			}
			return
		}
	}
}

func (t *MyTimer) Reset(duration ) {
	 <- duration
}

func (t *MyTimer) Stop() {
	 <- struct{}{}
}

func main() {
	timer := NewMyTimer(2 * )

	(1 * )
	(3 * )

	(2 * )
	()
	("Timer stopped")
}

In this example, MyTimer is a custom Timer implementation that supports reset and stop operations. MyTimer is implemented as the underlying layer and receives reset and stop signals through the channel.

Summarize

Timer in Go implements a scheduler that relies on unbuffered channels and Go. You can use or to create simple Timers, or to implement more complex Timer functions through custom structures.

This is the article about the detailed explanation of the implementation method of Timer without buffer in Go 1.23. For more related content related to Go implementation without buffer, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!