<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                讀取用戶的輸入 我們如何讀取用戶的鍵盤(控制臺)輸入呢?從鍵盤和標準輸入 os.Stdin 讀取輸入,最簡單的辦法是使用 fmt 包提供的 Scan 和 Sscan 開頭的函數。請看以下程序: 示例 1: 從控制臺讀取輸入: ~~~ package main import "fmt" var ( firstName, lastName, s string i int f float32 input = "56.12 / 5212 / Go" format = "%f / %d / %s" ) func main() { fmt.Println("Please enter your full name: ") fmt.Scanln(&firstName, &lastName) // fmt.Scanf("%s %s", &firstName, &lastName) fmt.Printf("Hi %s %s!\n", firstName, lastName) fmt.Sscanf(input, format, &f, &i, &s) fmt.Println("From the string we read: ", f, i, s) } ~~~ 使用 go build 編譯一個可執行二進制文件 ~~~ go build main.go ./main Please enter your full name: 鍵盤輸入: golang Murphy 返回: Hi golang Murphy! From the string we read: 56.12 5212 Go ~~~ Scanln 掃描來自標準輸入的文本,將空格分隔的值依次存放到后續的參數內,直到碰到換行。 Scanf 與其類似,除了 Scanf 的第一個參數用作格式字符串,用來決定如何讀取。 Sscan 和以 Sscan 開頭的函數則是從字符串讀取,除此之外,與 Scanf 相同。如果這些函數讀取到的結果與您預想的不同,您可以檢查成功讀入數據的個數和返回的錯誤。 您也可以使用 bufio 包提供的緩沖讀取(buffered reader)來讀取數據,正如以下例子所示: 示例 2: ~~~ package main import ( "bufio" "fmt" "os" ) var inputReader *bufio.Reader var input string var err error func main() { inputReader = bufio.NewReader(os.Stdin) fmt.Println("Please enter some input: ") input, err = inputReader.ReadString('\n') if err == nil { fmt.Printf("The input was: %s", input) } } ~~~ 使用 go build 編譯一個可執行二進制文件 ~~~ go build main.go ./main Please enter some input: 鍵盤輸入: hello world 返回: The input was: hello world ~~~ inputReader 是一個指向 bufio.Reader 的指針。inputReader := bufio.NewReader(os.Stdin) 這行代碼,將會創建一個讀取器,并將其與標準輸入綁定。 bufio.NewReader() 構造函數的簽名為:func NewReader(rd io.Reader) *Reader 該函數的實參可以是滿足 io.Reader 接口的任意對象(任意包含有適當的 Read() 方法的對象,請參考章節11.8),函數返回一個新的帶緩沖的 io.Reader 對象,它將從指定讀取器(例如 os.Stdin)讀取內容。 返回的讀取器對象提供一個方法 ReadString(delim byte),該方法從輸入中讀取內容,直到碰到 delim 指定的字符,然后將讀取到的內容連同 delim 字符一起放到緩沖區。 ReadString 返回讀取到的字符串,如果碰到錯誤則返回 nil。如果它一直讀到文件結束,則返回讀取到的字符串和 io.EOF。如果讀取過程中沒有碰到 delim 字符,將返回錯誤 err != nil。 在上面的例子中,我們會讀取鍵盤輸入,直到回車鍵(\n)被按下。 屏幕是標準輸出 os.Stdout;os.Stderr 用于顯示錯誤信息,大多數情況下等同于 os.Stdout。 一般情況下,我們會省略變量聲明,而使用 :=,例如: inputReader := bufio.NewReader(os.Stdin) input, err := inputReader.ReadString('\n') 我們將從現在開始使用這種寫法。 第二個例子從鍵盤讀取輸入,使用了 switch 語句: 示例 3: ~~~ package main import ( "bufio" "fmt" "os" ) func main() { inputReader := bufio.NewReader(os.Stdin) fmt.Println("Please enter your name:") input, err := inputReader.ReadString('\n') if err != nil { fmt.Println("There were errors reading, exiting program.") return } fmt.Printf("Your name is %s", input) // For Unix: test with delimiter "\n", for Windows: test with "\r\n" switch input { case "Philip\r\n": fmt.Println("Welcome Philip!") case "Chris\r\n": fmt.Println("Welcome Chris!") case "Ivo\r\n": fmt.Println("Welcome Ivo!") default: fmt.Printf("You are not welcome here! Goodbye!\r\n") } // version 2: switch input { case "Philip\r\n": fallthrough case "Ivo\r\n": fallthrough case "Chris\r\n": fmt.Printf("Welcome %s\n", input) default: fmt.Printf("You are not welcome here! Goodbye!\n") } // version 3: switch input { case "Philip\r\n", "Ivo\r\n": fmt.Printf("Welcome %s\n", input) default: fmt.Printf("You are not welcome here! Goodbye!\n") } } ~~~ 注意:Unix和Windows的行結束符是不同的! 使用 go build 編譯一個可執行二進制文件 ~~~ go build main.go ./main Please enter your name: 鍵盤輸入: Murphy 返回: Your name is Murphy You are not welcome here! Goodbye! You are not welcome here! Goodbye! You are not welcome here! Goodbye! ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看