Skip to content

Instantly share code, notes, and snippets.

@gokaybiz
Created March 6, 2019 19:28
Show Gist options
  • Save gokaybiz/e07872dbb112912523445d988880fc66 to your computer and use it in GitHub Desktop.
Save gokaybiz/e07872dbb112912523445d988880fc66 to your computer and use it in GitHub Desktop.
Arg calculator - Sum numbers
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
r := os.Args[1:] //remove info prefix
c := len(r) //len of numbers
t := 0 //total sum
nums := make([]int, c) //create int slice for conversion
if c > 0 {
/*for i := 0; i < c; i++ {
rr, _ := strconv.Atoi(r[i]) //str to int
t += rr //sum
}*/ // <-- Old version
for i, n := range r {
nums[i], _ = strconv.Atoi(n) //str to int then push int slice
t += nums[i] //sum
}
fmt.Println(t) //print result
} else {
fmt.Println("Sayi gir la!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment