package main import ( "bufio" "fmt" "math/rand" "os" ) var names = []string{} var err error func main() { if len(os.Args) < 2 || len(os.Args) > 2 { fmt.Println("Usage: ./file-reader names.txt") os.Exit(1) } filePath := os.Args[1] randWord := getWord(filePath) fmt.Printf("The chosen name is %v\n", randWord) } func getWord(path string) (word string) { file, err := os.Open(path) if err != nil { fmt.Println(err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { names = append(names, scanner.Text()) } randName := names[rand.Intn(len(names)-0)] return randName }