Skip to content

Instantly share code, notes, and snippets.

@alco
Created September 27, 2012 18:12
Show Gist options
  • Save alco/3795486 to your computer and use it in GitHub Desktop.
Save alco/3795486 to your computer and use it in GitHub Desktop.
Rust vs Go
/*
I'm sure this is not a complete working code. Some includes/imports must be missing.
*/
fn main() {
for ["Alice", "Bob", "Carol"].each |name| {
let name = copy *name;
do task::spawn {
let v = rand::Rng().shuffle([1, 2, 3]);
for v.each |num| {
io::print(fmt!("%s says: '%d'\n", name, *num))
}
}
}
}
/*
This is a complete working program, equivalent to the Rust program.
*/
package main
import (
"fmt"
"math/rand"
"time"
)
func shuffle(nums []int) []int {
result := make([]int, len(nums))
indices := rand.Perm(len(nums))
for i, si := range indices {
result[i] = nums[si]
}
return result
}
func main() {
people := []string{"Alice", "Bob", "Carol"}
for _, name := range people {
go func(name string) {
v := shuffle([]int{1, 2, 3})
for _, num := range v {
fmt.Printf("%s says: '%d'\n", name, num)
}
}(name)
}
time.Sleep(10 * time.Millisecond)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment