golang goroutine 骚操作系列
type Ch[T any] struct {
myCh chan T
}
func newCH[T any](lenght int) Ch[T] {
return Ch[T]{make(chan T, lenght)}
}
func (itself *Ch[T]) Set(data T) {
itself.myCh <- data
}
func (itself *Ch[T]) Get() T {
return <-itself.myCh
}
type OnlyRead[chanT any, T chan chanT] struct {
myCh T
}
func (itself *OnlyRead[T, chanT]) Read() T {
return <-itself.myCh
}
type OnlyWrite[chanT any, T chan chanT] struct {
myCh T
}
func (itself *OnlyWrite[T, chanT]) Write(data T) {
itself.myCh <- data
}
func newORW[chanT any, T chan chanT](ch T) (OnlyRead[chanT, T], OnlyWrite[chanT, T]) {
return OnlyRead[chanT, T]{ch}, OnlyWrite[chanT, T]{ch}
}
func main() {
or, ow := newORW(make(chan string, 10))
ow.Write("nihaonihao")
fmt.Println(or.Read())
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: