Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

A execução não aguarda input do usuário

No código abaixo:

package main

import "fmt"

func main() {

    showMenu()

    /*
        "%d" means we expect an integer (go figure).
        "&" means the variable address in memory, since Scanf does not expect a variable, but an address to one that already exists.
        & in front of any var, does not return the var, but the address

        fmt.Scanf("%d", &option)
    */
    option := 0

    fmt.Scan(&option) // fmt.Scan function (not Scanf) avoids passing the type expected for the input.

    fmt.Println("You chose the option", option)

    if option == 1 {

    } else if option == 2 {

    } else if option == 0 {
        fmt.Println("Good bye.")
    } else {
        fmt.Println("Invalid option, will exit.")
        return
    }
}

func showMenu() {
    fmt.Println("Welcome to this complex environment.")
    fmt.Println("Please choose an option:.")
    fmt.Println("1) Start monitoring")
    fmt.Println("2) Show logs")
    fmt.Println("0) Leave")
}

O programa não está aguardando qualquer input de usuário, tendo como saída o seguinte:

$ go run main.go
Welcome to this complex environment.
Please choose an option:.
1) Start monitoring
2) Show logs
0) Leave
You chose the option 0
Good bye.

Encontrei algo semelhante aqui (https://stackoverflow.com/questions/17401709/why-doesnt-fmt-scanf-in-go-wait-for-user-input), mas não me ajudou. Vocês já tiveram este problema antes?

1 resposta
solução!

Golang no Windows deve ser maluco. Fechei o VSC e abri novamente, e estava aguardando corretamente o meu input. Não fiz qualquer outra alteração.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software