这个go代码有什么问题,什么是os.Stdin? [关闭]

  1 package main                                                                                                          
  2                                                                                                                       
  3 import (                                                                                                              
  4         "bufio"                                                                                                       
  5         "fmt"                                                                                                         
  6         "os"                                                                                                          
  7 )                                                                                                                     
  8                                                                                                                       
  9 func main() {                                                                                                         
 10         input := bufio.NewScanner(os.Stdin)                                                                           
 11         if input.Scan == 1 {                                                                                          
 12                 fmt.println("true")                                                                                   
 13         }                                                                                                             
 14 }     

I want create something that will ask for user input, then check if that user input = 1

The Scan code documentation says:

//Scan advances the Scanner to the next token, which will then be
//available through the Bytes or Text method. It returns false when the
//scan stops, either by reaching the end of the input or an error.

So you could do something like this:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    input := bufio.NewScanner(os.Stdin)
    if input.Scan() && input.Text() == "1" {
        fmt.Println("true")
    }
}

The os.Stdin is how you make your Scanner get it's input from the stdin. (https://en.wikipedia.org/wiki/Standard_streams#/media/File:Stdstreams-notitle.svg)

One note, pay attention for uppercase letters for exported functions. On line 12 you wrote

fmt.println  

and it should be

fmt.Println

You should go to https://tour.golang.org/welcome/1 to get started with golang.