1. 程式人生 > >5.1 標準輸入 NewScanner

5.1 標準輸入 NewScanner

tdi text %d source lin %x ces Go sca

package main

import (
    "fmt"
)

func main() {

    var name string
    fmt.Println("What is your name?")
    fmt.Scanf("%s\n", &name)

    var age int
    fmt.Println("What is your age?")
    fmt.Scanf("%d\n", &age)

    fmt.Printf("Hello %s, your age is %d\n", name, age)

}

/*
What is your name?
dsf What is your age? 11 Hello dsf, your age is 11 */

package main

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

func main() {

    // The Scanner is able to
    // scan input by lines
    sc := bufio.NewScanner(os.Stdin)
    for sc.Scan() {
        txt := sc.Text()
        fmt.Printf("Echo: %s\n", txt)
    }

}

/*
hh Echo: hh hh Echo: hh jhbhj Echo: jhbhj 一直捕捉屏幕輸入 然後輸出 */

package main

import (
    "fmt"
    "os"
)

func main() {

    for {
        data := make([]byte, 8)
        n, err := os.Stdin.Read(data)
        if err == nil && n > 0 {
            process(data)
        } else {
            break
} } } func process(data []byte) { fmt.Printf("Received: %X %s\n", data, string(data)) } /* h Received: 680A000000000000 h jj Received: 6A6A0A0000000000 jj klk裏面了昆明 Received: 6B6C6BE9878CE99D klk裏? Received: A2E4BA86E69886E6 ?了昆? Received: 988E0A0000000000 ?? 可能離開 Received: E58FAFE883BDE7A6 可能? Received: BBE5BC800A000000 ?開 jk Received: 6A6B0A0000000000 jk mm Received: 6D6D0A0000000000 mm */

5.1 標準輸入 NewScanner