1

In this code, I am trying to print the name and the address both at the same time. I globally declared a combined variable. But it is giving me the error, expected declaration, found combined and the code is not executing.

If I use the combined variable locally, then I am not able to use this variable in another function that is addCustomer().

combined := make(map[string]string)
func oneLine(key string) {
    scanner := bufio.NewScanner(os.Stdin)
    if scanner.Scan() {
        line := scanner.Text()
        combined[key] = line
        fmt.Println(combined[key])
    }
}

func addCustomer() {
    fmt.Printf("Enter your name: ")
    oneLine("name")

    fmt.Printf("Enter your address: ")
    oneLine("address")

    fmt.Println(fmt.Sprintf("Here is a name and address: [%s, %s]", combined["name"], combined["address"]))
}
Qora bhai
  • 47
  • 6
  • You can't use short variable declaration at the top level. Declare it like this: `var combined = make(map[string]string)` – icza May 01 '21 at 10:41

0 Answers0