Files
vRp.CD2g_test/utils/super/channel.go

23 lines
621 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package super
// TryWriteChannel 尝试写入 channel如果 channel 无法写入则忽略,返回是否写入成功
// - 无法写入的情况包括channel 已满、channel 已关闭
func TryWriteChannel[T any](ch chan<- T, data T) bool {
select {
case ch <- data:
return true
default:
return false
}
}
// TryWriteChannelByHandler 尝试写入 channel如果 channel 无法写入则执行 handler
// - 无法写入的情况包括channel 已满、channel 已关闭
func TryWriteChannelByHandler[T any](ch chan<- T, data T, handler func()) {
select {
case ch <- data:
default:
handler()
}
}