This is a creation in Article, where the information may have evolved or changed.
golang rich i/o---- With n species
Golang is the best language I have used at the moment, a touch will deeply love, and constantly practice, love for a long time. One reason is simplicity, power, and ease of use. Programming operations involve the highest frequency of I/O, and the standard IO package provides two interfaces (io). Reader and io. Writer) is a great unified abstraction of I/O , and will be simple, powerful, easy-to-use features reflect the most vividly. The two interfaces are defined as follows:
Typereaderinterface { Read (p []byte) (n int, err error)}typewriterinterface { Write (p []byte) (n int, err error) }
i/o feature. Below with n output " hello , < Span lang= "en-US" >world! " To feel the next.
Package Mainimport ("Bufio" "bytes" "FMT" "io" "Log" "Mime/quotedprintable" "OS" "Strings" "Te Xt/tabwriter ") Func main () {//1 fmt. Println ("Hello, world!") 2 io. WriteString (OS. Stdout, "Hello, world!\r\n") os. Stdout.writestring ("Hello, world!\r\n")//3 w: = Bufio. Newwriter (OS. Stdout) fmt. Fprint (W, "Hello,") fmt. Fprint (W, "world!\r\n") W.flush ()//Don ' t forget to flush! Fmt. Fprint (OS. Stdout, "Hello, world!\r\n")//4 r: = Strings. Newreader ("Hello, world!\r\n") If _, err: = Io. Copy (OS. Stdout, R); Err! = Nil {log. Fatal (Err)} r1: = Strings. Newreader ("Hello, world!\r\n") buf: = Make ([]byte, 8)//BUF are used here ... if _, err: = Io. CopyBuffer (OS. Stdout, R1, BUF); Err! = Nil {log. Fatal (ERR)} r2: = Strings. Newreader ("Hello, world!\r\n")//buf: = Make ([]byte, 8) If _, err: = Io. Copyn (OS. Stdout, R2, int64 (R2. Len ())); Err! = Nil {log. Fatal (Err)}//5 var b bytes. BuFfer//A Buffer needs no initialization. B.write ([]byte ("Hello,")) FMT. fprintf (&b, "world!\r\n") B.writeto (OS. Stdout)//Output:hello world! 6 Wtab: = Tabwriter. Newwriter (OS. Stdout, 1, 1, 1, ', Tabwriter. AlignRight) defer Wtab.flush () wtab.write ([]byte ("Hello, world!\r\n"))//7 wquote: = quotedprintable. Newwriter (OS. Stdout) Wquote.write ([]byte ("Hello, world!\r\n")) Wquote.write ([]byte ("These symbols would be escaped: = \ T")) Wqu Ote. Close () Wquote.write ([]byte ("\ r \ n"))//8 log: = log. New (OS. Stdout, "", 0) log. Println ("Hello, world!")}
The above code is from go source, compile run output is as follows:
Hello, world!. Hello, world!. Hello, world!. Hello, World!hello, World!hello, World!hello, World!hello, world! Hello, world!. Hello, world!. Hello, world!. These symbols'll be escaped: =3d =09hello, world!
The first is very common,
This form of Hello world is shown in various go language books .
The second type of IO package and OS package provides the writestring function or method to io. Writer is encapsulated.
The third is FMT fprint function, similar to the first one. From go source can be seen Print println fprint Fprintln function.
Func Print (A ... interface{}) (n int, err error) { return fprint (OS). Stdout, a ...)} Func Println (A ... interface{}) (n int, err error) { return fprintln (OS). Stdout, a ...)}
fourth is Io copy function: io. Copy < Span lang= "en-us" >io. CopyBuffer < Span lang= "en-us" >io. Copyn copybuffer function encapsulation,
CopyBuffer is the actual implementation of Copy and copybuffer.//if buf are nil, one is Allocated.func copyBuffer (DST W Riter, src Reader, buf []byte) (written int64, err error)
CopyBuffer The function reads the data from reader with the BUF buffer and writes it to writer .
The fifth is the method provided by the bytes package, which encapsulates the Writer method.
WriteTo writes data to W until the buffer was drained or an error occurs.//the return value n is the number of bytes WR Itten; It always fits into an//int, but it's int64 to match the IO. Writerto interface. Any error//encountered during the write is also Returned.func (b *buffer) writeto (w io. Writer) (n Int64, err error)
The sixth type is the IO that the text package implements . Writer Interface, thetext/tabwriter Package enables text column-aligned output.
Write writes BUF to the writer b.//The only errors returned is ones encountered//while writing to the underlying out Put Stream.//func (b *writer) Write (buf []byte) (n int, err error) {
The seventh type is the IO implemented by the "mime/quotedprintable" package . Writer interface.
Write encodes p using quoted-printable encoding and writes it to the//underlying IO. Writer. It limits line length to characters. The encoded//bytes is not necessarily flushed until the Writer is Closed.func (w *writer) Write (P []byte) (n int, err ER ROR) {
The eighth type is also more commonly used, provided by the log packet.
so many helloworld The may not be comprehensive, but it's the most written language I've ever seen.