Golang reads a row from the stdin

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This article goes from life's Leanote Blog: http://leanote.com/blog/view/531706971a91084358000000

Fmt. SCANLN () does not read a line, but instead encounters a line break and stops,

SCANLN is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF.

As an example:

var I, J, K Intfmt. Scanln (&i, &j, &k) fmt. Println (I, J, K)

Enter 1, 2 return to the car after the program ends. If you switch to FMT. Scan () will also let the input.

Use stdin to read a row of data together with reader:

Reader: = Bufio. Newreader (OS. Stdin) Strbytes, hasmore, err: = Reader. ReadLine ()

But Bufio with FMT. Scan () can have errors together, may be bufio problems, Bufio multiple reads, leading to FMT. Scan () cannot read

or write yourself a way to read a line:

Get a row of Func scanline () string {var c bytevar err Errorvar b []bytefor; err = nil; {_, Err = Fmt. SCANF ("%c", &c) if c! = ' \ n ' {b = append (b, c)} else {break;}} return string (b)}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.