Created
March 6, 2019 19:28
-
-
Save gokaybiz/e07872dbb112912523445d988880fc66 to your computer and use it in GitHub Desktop.
Arg calculator - Sum numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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