Source

go-play / fib.go

Full commit
package main

import (
	"flag"
	"fmt"
	"os"
	"strconv"
)

func fib(n int) []uint64 {
	var a, b uint64
	fibs := make([]uint64, n)
	a, b = 1, 1
	for i := 0; i < n-1; i++ {
		fibs[i] = a
		a, b = b, a+b
	}
	fibs[n-1] = a

	return fibs
}

func main() {
	flag.Parse()

	if flag.NArg() != 1 {
		fmt.Println("error: wrong number of arguments")
		os.Exit(1)
	}

	n, err := strconv.Atoi(flag.Arg(0))
	if err != nil {
		fmt.Println("error: bad number -", flag.Arg(0))
		os.Exit(1)
	}

	fmt.Println(fib(n))
}