0

Here is the Go code I was working with, a nice example of concurrent computing ...

// sieve.go
// a concurrent prime sieve
// the prime sieve: Daisy-chain Filter processes

package main

import "fmt"

// send the sequence 2, 3, 4, ... to channel 'ch'
func Generate(ch chan<- int) {
    for i := 2; ; i++ {
        // send 'i' to channel 'ch'
        ch <- i
    }
}

// copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func Filter(in <-chan int, out chan<- int, prime int) {
    for {
        // receive value from channel 'in'
        i := <-in
        if i%prime != 0 {
            // send value 'i' to channel 'out'
            out <- i
        }
    }
}

func main() {
    // create a new channel
    ch := make(chan int)
    // launch Generate goroutine
    go Generate(ch)
    // get the first 10 primes
    for i := 0; i < 10; i++ {
        prime := <-ch
        fmt.Println(prime)
        ch1 := make(chan int)
        go Filter(ch, ch1, prime)
        ch = ch1
    }
}

/*
2
3
5
7
11
13
17
19
23
29
*/

The for loop is the only loop in Go but it covers all the possible loops.

Edited by vegaseat

0
Comparing C/C++ and Go types:
+--------------------+------------------+
| C/C++ type         | Go type          |
+--------------------+------------------+
| bool               | bool             |
| char               | byte             |
| signed char        | int8             |
| unsigned char      | byte             |
| short              | int16            |
| unsigned short     | uint16           |
| int                | int              |
| unsigned int       | uint             |
| long               | int32 or int64   |
| unsigned long      | uint32 or uint64 |
| long long          | int64            |
| unsigned long long | uint64           |
| float              | float32          |
| double             | float64          |
| char * or char []  | string           |
+--------------------+------------------+
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.