A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Factorial in Go page! Here, you'll find the source code for this program as well as a description of how the program works.
package main
import (
"fmt"
"os"
"strconv"
)
func factorial(n uint64) uint64 {
if n <= 0 {
return 1
}
return n * factorial(n-1)
}
func exitWithError(msg string) {
fmt.Println(msg)
os.Exit(1)
}
func main() {
if len(os.Args) != 2 {
exitWithError("Usage: please input a non-negative integer")
}
n, err := strconv.ParseUint(os.Args[1], 0, 64)
if err != nil {
exitWithError("Usage: please input a non-negative integer")
}
if n > 65 {
exitWithError(fmt.Sprintf("!%d is out of the reasonable bounds for calculation", n))
}
fmt.Println(factorial(n))
}
Factorial in Go was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.