Fibonacci Example Using Channels and Go Routines

221 views
Skip to first unread message

Jack S.

unread,
Nov 16, 2009, 1:15:14 PM11/16/09
to golang-nuts
For the past day or two, I've been playing with Go. I have experience
with Limbo, so I thought that I might share some of my knowledge with
others as Go came pretty easily to me. I created a well documented
Fibonacci calculator using Go Routines and Channels (instead of the
_very_ recursive solution). In any case, it might be a good primer
for people who are interested in it.

There are a few limitations (I'm in the process of removing them), but
it's still a nice primer I think Enjoy!

Code can be found at http://www.re-cycledair.com/downloads/fibonacci.go

/Jack

Russ Cox

unread,
Nov 17, 2009, 12:24:32 AM11/17/09
to Jack S., golang-nuts
On Mon, Nov 16, 2009 at 10:15, Jack S. <jack.sli...@gmail.com> wrote:
> Code can be found at http://www.re-cycledair.com/downloads/fibonacci.go

Looks nice. I just want to point out two places that are not
quite as idiomatic Go as they could be. First, none of the parens
you have on if conditions are necessary, not even the ones
in the branches of the && expression. Running gofmt -w fibonacci.go
will remove the top-level ones for you; you'll have to remove the
ones in the && yourself. Second, the sequence of if statements
checking various conditions can be written as a switch:

switch {
//Make sure the conversion went correctly, otherwise return failure.
case err != nil:
return -1, "Invalid argument. Argument must be an integer.\n"

//Since this implementation is limited, make sure the user can't go
//beyond the program's limits.
case goal > 46:
return -1, "This program only calculates up to the 46th Fibonacci number.\n"

//Check the lower bound as well.
case goal < 1:
return -1, "Invalid range. Number must be >= 1.\n"
}

Hope you're having fun writing Go programs.
Russ

Jack S.

unread,
Nov 17, 2009, 9:19:24 AM11/17/09
to golang-nuts
I hadn't realized that switch statements were the preferred way of
doing things in Go. In some circles they're frowned on :( Also,
thanks for the heads up on the "gofmt -w", that will make life a
little easier.

On Nov 17, 12:24 am, Russ Cox <r...@golang.org> wrote:
> On Mon, Nov 16, 2009 at 10:15, Jack S. <jack.slingerl...@gmail.com> wrote:
> > Code can be found athttp://www.re-cycledair.com/downloads/fibonacci.go
>
> Looks nice.  I just want to point out two places that are not
> quite as idiomatic Go as they could be.  First, none of the parens
> you have on if conditions are necessary, not even the ones
> in the branches of the && expression.  Running gofmt -wfibonacci.go
> will remove the top-level ones for you; you'll have to remove the
> ones in the && yourself.  Second, the sequence of if statements
> checking various conditions can be written as a switch:
>
>         switch {
>         //Make sure the conversion went correctly, otherwise return failure.
>         case err != nil:
>                 return -1, "Invalid argument.  Argument must be an integer.\n"
>
>         //Since this implementation is limited, make sure the user can't go
>         //beyond the program's limits.
>         case goal > 46:
>                 return -1, "This program only calculates up to the 46thFibonaccinumber.\n"
Reply all
Reply to author
Forward
0 new messages