Run or Go: Python meets C++.

Where Python meets C++.
Post Reply
KBleivik
Site Admin
Posts: 184
Joined: Tue Sep 29, 2009 6:25 pm
Location: Moss Norway
Contact:

Run or Go: Python meets C++.

Post by KBleivik »

The languages home page: http://golang.org/

Install: http://golang.org/doc/install.html

Tutorial: http://golang.org/doc/go_tutorial.html

See also:

http://code.google.com/p/go/

http://techcrunch.com/2009/11/10/google-go-language/

http://groups.google.com/group/golang-dev

http://groups.google.com/group/golang-checkins?lnk=srg

Example: Go code for "a concurrent prime sieve":

Code: Select all

// A concurrent prime sieve
// See "Prime Numbers" section of the tutorial:
// http://golang.org/doc/go_tutorial.html
package main
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func Generate(ch chan<- int) {
 for i := 2; ; i++ {
  ch <- i // Send 'i' to channel 'ch'.
 }
}
// 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 {
  i := <-in // Receive value from 'in'.
  if i%prime != 0 {
   out <- i // Send 'i' to 'out'.
  }
 }
}
// The prime sieve: Daisy-chain Filter processes.
func main() {
 ch := make(chan int) // Create a new channel.
 go Generate(ch)      // Launch Generate goroutine.
 for i := 0; i < 10; i++ {
  prime := <-ch
  print(prime, "\n")
  ch1 := make(chan int)
  go Filter(ch, ch1, prime)
  ch = ch1
 }
}
Source: http://golang.org/

Related:

http://mathworld.wolfram.com/PrimeFacto ... ithms.html

http://stackoverflow.com/questions/4537 ... me-numbers

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests