<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 開頭的函數。請看以下程序: 示例 12.1?[readinput1.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_12/readinput1.go): ~~~ // 從控制臺讀取輸入: 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) // Hi Chris Naegels fmt.Sscanf(input, format, &f, &i, &s) fmt.Println("From the string we read: ", f, i, s) // 輸出結果: From the string we read: 56.12 5212 Go } ~~~ `Scanln`?掃描來自標準輸入的文本,將空格分隔的值依次存放到后續的參數內,直到碰到換行。`Scanf`?與其類似,除了`Scanf`?的第一個參數用作格式字符串,用來決定如何讀取。`Sscan`?和以?`Sscan`?開頭的函數則是從字符串讀取,除此之外,與?`Scanf`?相同。如果這些函數讀取到的結果與您預想的不同,您可以檢查成功讀入數據的個數和返回的錯誤。 您也可以使用?`bufio`?包提供的緩沖讀取(buffered reader)來讀取數據,正如以下例子所示: 示例 12.2?[readinput2.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_12/readinput2.go): ~~~ package main import ( "fmt" "bufio" "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\n", input) } } ~~~ `inputReader`?是一個指向?`bufio.Reader`?的指針。`inputReader := bufio.NewReader(os.Stdin)`?這行代碼,將會創建一個讀取器,并將其與標準輸入綁定。 `bufio.NewReader()`?構造函數的簽名為:`func NewReader(rd io.Reader) *Reader` 該函數的實參可以是滿足?`io.Reader`?接口的任意對象(任意包含有適當的?`Read()`?方法的對象,請參考[章節11.8](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/11.8.md)),函數返回一個新的帶緩沖的?`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`?語句: 示例 12.3?[switch_input.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_12/switch_input.go): ~~~ package main import ( "fmt" "os" "bufio" ) 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!") } // 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的行結束符是不同的! **練習** **練習 12.1:**?[word_letter_count.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_12/word_letter_count.go) 編寫一個程序,從鍵盤讀取輸入。當用戶輸入 'S' 的時候表示輸入結束,這時程序輸出 3 個數字: i) 輸入的字符的個數,包括空格,但不包括 '\r' 和 '\n' ii) 輸入的單詞的個數 iii) 輸入的行數 **練習 12.2:**?[calculator.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_12/calculator.go) 編寫一個簡單的逆波蘭式計算器,它接受用戶輸入的整型數(最大值 999999)和運算符 +、-、*、/。 輸入的格式為:number1 ENTER number2 ENTER operator ENTER --> 顯示結果 當用戶輸入字符 'q' 時,程序結束。請使用您在練習11.3中開發的?`stack`?包。
                  <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>

                              哎呀哎呀视频在线观看