Hello Go

Gopher saying “Hello world!”

Saying Hello

There are a bunch of ways we could get to an output that contains “Hello world!” Preferably this would include a blockchain and neural network on a serverless architecture for maximum hype factor but, doing that would be a complete overkill and not to mention over budget for doing something as simple as printing to standard output.

A flowchart of a typical hello world program
// hello.go
package mainimport (
  "fmt"
  "math/rand"
)func main() {
  t := "Hello World!"
  s := []rune(t)
  
  for {
    rand.Shuffle(len(s), func(i int, j int) {
      s[i], s[j] = s[j], s[i]
    })    if string(s) == t {
      break
    }
  }  fmt.Println(string(s))
}

Tracking Progress

We got the basics down but it would be nice to have some kind of progress log with the permutation count and value so let’s add that.

// hello.go
package mainimport (
  "fmt"
  "math/rand"
)func main() {
  t := "Hello World!"
  s := []rune(t)  for {
    rand.Shuffle(len(s), func(i int, j int) {
      s[i], s[j] = s[j], s[i]
    })    fmt.Println(string(s))    if string(s) == t {
      break
    }
  }
}
$ hello | cat -n

Many Is More Than One

Our program is actually already fairly quick to compute considering what it needs to do, the garbage collector isn’t bogging us down much either despite the fact that we constantly are creating new strings.

A flow chart of Gopher Sort
// hello.main
package mainimport (
  "fmt"
  "math/rand"
)func main() {
  t := "Hello world!"
  c := make(chan string)  for i := 0; i < 32; i++ {
    go gopher(t, c)
  }  for s := range c {
    fmt.Println(s)    if s == t {
      break
    }
  }
}func gopher(t string, c chan string) {
  s := []rune(t)  for {
    rand.Shuffle(len(s), func(i, j int) {
      s[i], s[j] = s[j], s[i]
    })    c <- string(s)
  }
}

How Many Is A Crowd?

What kind of performance gains did we get from this? Well we’ve actually degraded the performance.

A plot of Gopher efficiency at performing Gopher Sort

Conclusion

Our Gophers can’t work well on their own since they’re not sure if they said the right thing, so in this case adding more screaming Gophers did not help us out much.

Commit Log

Contains Commits, Duh!

Unlisted

Casper Beyer

Written by

A slightly bitter bearded developer with a background in fine arts.

Commit Log

Contains Commits, Duh!