String 1 - Enter the GO language learning notes with spaces

GO recently started learning the language, and did some programming exercises. Ethics questions to the input string with spaces, it is very easy to implement in C / C ++ but GO seemed not so easy. Learned C / C ++ may know, can be used in C gets () function can be used in C ++ getline () function to implement the input string with spaces. That we may have to ask if there is a similar function in GO? the answer is negative.

In addition to the GO and fmt package os, we can also be implemented using bufio buffered input and output.
How do we read user data from the keyboard (console) input? Input means to read data from a standard keyboard or other input (os.Stdin).

1. fmt Scan_ bag or series of functions that are input Sscan_

package main

import "fmt"

var (
    firstName, lastName string
    str1, str2, str3 string
    num int
    str = "We / love / Go / 1314"
    format = "%s / %s / %s / %d"
)

func main() {
    fmt.Println("Please input your full name: ")
   // 使用Scan输入
    fmt.Scan(&firstName, &lastName)
    fmt.Printf("Hi %s %s!\n", firstName, lastName)

    fmt.Println("Please again input your full name: ")
    // 使用Scanln输入
    fmt.Scanln(&firstName, &lastName)
    fmt.Printf("Hi %s %s!\n", firstName, lastName)

    fmt.Println("Please again input your full name: ")

    // 使用Scanf输入
    fmt.Scanf("%s %s", &firstName, &lastName)
    fmt.Printf("Hi %s %s!\n", firstName, lastName)

    fmt.Println("From the str we read: ")

    // 使用Sscanf读取
    fmt.Sscanf(str, format, &str1, &str2, &str3, &num)
    fmt.Println(str1, str2, str3, num)
}

If the input is:

xiao hua
xiao ming
xiao hong

The output is:

Please input your full name: 
xiao hua
Hi xiao hua!
Please again input your full name: 
xiao ming
Hi xiao ming!
Please again input your full name: 
xiao hong
Hi xiao hong!
From the str we read: 
We love Go 1314
Scan () function prototype is:
func Scan(a ...interface{}) (n int, err error) {
    return Fscan(os.Stdin, a...)
}

Blank Scan scan text from standard input, to successfully read values ​​are stored into the separated parameter to this function is successfully delivered. Line breaks as white space. Any error returned successfully scanned and the number of entries encountered. If the entry reads less than the supplied parameters, it will return an error report reason.

Scanln () function prototype is:
func Scanln(a ...interface{}) (n int, err error) {
    return Fscanln(os.Stdin, a...)
}

Scanln similar Scan, but it will stop scanning at line breaks, there must be a newline or EOF after the last entry.

Scanf () function prototype is:
func Scanf(format string, a ...interface{}) (n int, err error) {
    return Fscanf(os.Stdin, format, a...)
}

Blank Scanf scanned text from standard input, according to the specified format parameter format successfully read values are stored into the separated parameter to this function is successfully delivered. Any error returned successfully scanned and the number of entries encountered.
Newline character in the input must match the format line breaks.

Sscanf () function prototype is:
func Sscanf(str string, format string, a ...interface{}) (n int, err error) {
    return Fscanf((*stringReader)(&str), format, a...)
}

Blank Sscanf from scanned text string str, according to the specified format parameter format successfully read values are stored into the separated parameter to this function is successfully delivered. Any error returned successfully scanned and the number of entries encountered.
Newline character in the input must match the format line breaks.

Scan and Scanln difference method:
  • Content about Scan function is space, even if there is a line break will not affect Scan access to content.
  • Scanln function will identify content about space, but in the event of a line break will end immediately, regardless of whether there is follow-up also need to enter content.

2. bu fi o bag buffered Reader with spaces for input string

package main

import (
    "bufio"
    "fmt"
    "os"
    )
//var inputReader *bufio.Reader
//var input string
//var err error

func main() {

    inputReader := bufio.NewReader(os.Stdin)    // 使用了自动类型推导,不需要var关键字声明
    //inputReader = bufio.NewReader(os.Stdin)

    fmt.Println("Please input your name: ")
    
    //input, err = inputReader.ReadString('\n')
    input, err := inputReader.ReadString('\n')

    if err == nil {
        fmt.Printf("Your name is: %s\n", input)
    }
}

If the input is:

xiao hua

The output is:

Please input your name: 
xiao hua
Your name is: xiao hua

In the embodiment, inputReader is a pointer to a class Reader bu fi o, then the main function, by bufio.NewReader (os.Stdin)
creates a bu ff er Reader, and coupled to the variable inputReader.

bufio.NewReader () function prototype:
func NewReader(rd io.Reader) *Reader {
    return NewReaderSize(rd, defaultBufSize)
}

Laws with any object can be used as an interface io.Reader bu fi o.NewReader () in the parameters, and returns a new buffered io.Reader, os.Stdin meet this condition. The Reader buffered there is a way ReadString (delim byte), this method would have been to read the data until it reaches the specified termination character (delim byte) before the end of input, terminator will become part of the input into the bu ff er in together.

RearString () function prototype is:
func (b *Reader) ReadString(delim byte) (string, error) {
    bytes, err := b.ReadBytes(delim)
    return string(bytes), err
}

ReadString return value is a string and read nil. If no specific terminator (delim byte) read at the end, returns a err! = Nil. In the above example, we typed from the keyboard until the "\ n" until the end. The screen is the standard output os.Stdout, error information is written os.Stderr. In most cases, os.Stderr equivalent os.Stdout.

In general, the code GO's variable declaration are omitted, and the direct use of ": =" automatic type derivation, such as:

inputReader := bufio.NewReader(os.Stdin) 
input, err := inputReader.ReadString('\n')

3. Summary

GO usage in Scan_ or Sscan_ series of functions and system functions scan_ C is similar, learned C people can quickly learn to use these functions. But to realize the input string with spaces will be employed os.Stdin Reader os bag and the bag bu fi o buffered.


Homepage:

www.codeapes.cn

Guess you like

Origin www.cnblogs.com/codeapes666/p/12093790.html