channel

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

channel is a simple communication hub for go routines

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Communication

type Communication struct {
	// contains filtered or unexported fields
}

func Init

func Init() *Communication

Init creates a new Communication

Example
package main

import (
	"fmt"
	"sync"

	"simonwaldherr.de/go/golibs/channel"
)

func main() {
	var w sync.WaitGroup
	var receiver = make(map[int]chan interface{})

	w.Add(5)

	con := channel.Init()

	for i := 0; i < 5; i++ {
		receiver[i] = con.AddReceiver()

		go func(j int) {
			fmt.Println(<-receiver[j])
			w.Done()
		}(i)
	}

	transmitter01 := con.AddTransmitter()

	transmitter01 <- "Hello World"

	w.Wait()

	for i := 0; i < 5; i++ {
		con.CloseReceiver(receiver[i])
	}
}
Output:

Hello World
Hello World
Hello World
Hello World
Hello World

func (*Communication) AddReceiver

func (hub *Communication) AddReceiver() chan interface{}

AddReceiver adds a new receiver to the hub

func (*Communication) AddTransmitter

func (hub *Communication) AddTransmitter() chan<- interface{}

AddTransmitter adds a new transmitter to the hub

func (*Communication) Close added in v0.15.0

func (hub *Communication) Close()

Close closes the hub

func (*Communication) CloseAll added in v0.15.0

func (hub *Communication) CloseAll()

CloseAll closes all receivers and transmitters

func (*Communication) CloseReceiver

func (hub *Communication) CloseReceiver(ch chan interface{}) int

CloseReceiver closes a receiver

func (*Communication) CloseTransmitter added in v0.15.0

func (hub *Communication) CloseTransmitter(ch chan<- interface{})

CloseTransmitter closes a transmitter

func (*Communication) CountReceiver

func (hub *Communication) CountReceiver() int

CountReceiver returns the number of receivers

func (*Communication) Send added in v0.15.0

func (hub *Communication) Send(message interface{})

Send sends a message to all receivers

func (*Communication) SendTo added in v0.15.0

func (hub *Communication) SendTo(message interface{}, receiver chan interface{})

SendTo sends a message to a specific receiver

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL