Exercises & activities from the go workshop provided by Packt: https://courses.packtpub.com/courses/go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

46 lines
714 B

package main
import (
"fmt"
"os"
)
func getPassedArgs(minArgs int) []string {
if len(os.Args) < minArgs {
fmt.Printf("At least %v arguments are needed\n", minArgs)
os.Exit(1)
}
var args []string
for i := 1; i < len(os.Args); i++ {
args = append(args, os.Args[i])
}
return args
}
func findLongest(args []string) string {
var longest string
for i := 0; i < len(args); i++ {
if len(args[i]) > len(longest) {
longest = args[i]
}
}
return longest
}
func main() {
if longest := findLongest(getPassedArgs(3)); len(longest) > 0 {
fmt.Println("The longest word passed was:", longest)
} else {
fmt.Println("There was an error")
os.Exit(1)
}
}