Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
fix(go/fib): ensure example consistency
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed Aug 2, 2022
1 parent 2cdcb12 commit 390c4ca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
15 changes: 15 additions & 0 deletions Go/fibonacci/Enarx.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Arguments
args = [
"7",
"21"
]

# Pre-opened file descriptors
[[files]]
kind = "stdin"

[[files]]
kind = "stdout"

[[files]]
kind = "stderr"
58 changes: 33 additions & 25 deletions Go/fibonacci/main.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
package main

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

func FibonacciSequence(n uint64) uint64 {
if n <= 1 {
return n
}
return FibonacciSequence(n-1) + FibonacciSequence(n-2)
func init() {
log.SetFlags(0)
}

func fib(n uint64) uint64 {
if n <= 1 {
return n
}
return fib(n-1) + fib(n-2)
}

func main(){
if len(os.Args) > 1 {
for _, arg := range os.Args[1:] {
n, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
fmt.Printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n));
}
} else {
var input string
fmt.Print("Which Fibonacci index to find? ")
fmt.Scanln(&input)
n, err := strconv.ParseUint(input, 10, 64)
func main() {
flag.Parse()

args := flag.Args()
if len(args) == 0 {
fmt.Println("Enter a non-negative number:")
sc := bufio.NewScanner(os.Stdin)
sc.Scan()
b, err := sc.Bytes(), sc.Err()
if err != nil {
log.Fatal("Failed to read stdin: %s", err)
}
args = []string{string(b)}
}

for _, arg := range args {
n, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(-1)
log.Fatalf("Failed to parse number: %s", err)
}
fmt.Printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n));
}
fmt.Printf("Fibonacci sequence number at index %d is %d\n", n, fib(n))
}
}
5 changes: 2 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

fibonacci-go-wasm = final.stdenv.mkDerivation rec {
pname = "fibonacci";
version = "0.1.0";
version = "0.2.0";

src = "${self}/Go/fibonacci";

Expand All @@ -137,8 +137,7 @@
name = final.fibonacci-go-wasm.pname;

wasm = "${final.fibonacci-go-wasm}/bin/fibonacci.wasm";
# TODO: Read this from repo
conf = defaultConf final;
conf = "${self}/Go/fibonacci/Enarx.toml";
};

fibonacci-rust-wasm = naersk-lib.buildPackage {
Expand Down

0 comments on commit 390c4ca

Please sign in to comment.