码迷,mamicode.com
首页 > 其他好文 > 详细

Golang丰富的I/O----用N种Hello World展示

时间:2017-11-28 20:35:20      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:ble   --   感受   定义   col   ati   ted   count   turn   

Golang丰富的I/O----NHello World展示

Golang是我目前用过的最好的语言,一接触便深深地喜爱,不断实践,喜爱之情日久弥深。原因之一便是简单、强大、易用。编程操作涉及频率最高的莫过于I/O,标准io包提供的两个接口(io.Readerio.Writer)对I/O进行了伟大的统一抽象,将简单、强大、易用的特点体现地淋漓尽致。两个接口的定义如下:

typeReaderinterface {

    Read(p []byte) (n int, err error)

}


typeWriterinterface {

    Write(p []byte) (n int, err error)

}

  

标准库中的多个包实现了这两个接口,从而提供了丰富而强大的I/O功能。下面用N种输出Helloworld!”来感受下。

package main


import (

    "bufio"

    "bytes"

    "fmt"

    "io"

    "log"

    "mime/quotedprintable"

    "os"

    "strings"

    "text/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 is 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 will be escaped: = \t"))

    wQuote.Close()

    wQuote.Write([]byte("\r\n"))


    //8

    log := log.New(os.Stdout, "", 0)

    log.Println("Hello, world!")

}

  

以上代码均来自go源码,编译运行输出如下:

hello, world!

Hello, World!

Hello, World!

Hello, world!

hello, world!

hello, world!

hello, world!

hello, world!

Hello, world!

Hello, world!

Hello, world!

These symbols will be escaped: =3D =09

Hello, world!

  

第一种很常见,

fmt.Println("hello, world!") 

  

各种go语言书籍中均展示了该种形式的Hello World


第二种是io包和os包提供的WriteString函数或方法,对io.Writer进行了封装。


第三种是fmt包提供的Fprint函数,与第一种类似。从go源码可以看出PrintPrintln分别是对FprintFprintln函数的封装。

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...)

}

  

第四种是io包提供的三个copy函数:io.Copyio.CopyBufferio.CopyN。这三个函数是对copyBuffer函数的封装,

// copyBuffer is the actual implementation of Copy and CopyBuffer.

// if buf is nil, one is allocated.

func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)

  

copyBuffer函数借助buf缓冲从Reader读取数据然后写入到Writer中。


第五种是bytes包提供的方法,对Writer方法进行了封装。

// WriteTo writes data to w until the buffer is drained or an error occurs.

// The return value n is the number of bytes written; it always fits into an

// int, but it is 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)

  


第六种是text包实现的io.Writer接口,text/tabwriter包可以实现文本列对齐输出。

// Write writes buf to the writer b.

// The only errors returned are ones encountered

// while writing to the underlying output stream.

//

func (b *Writer) Write(buf []byte) (n int, err error) {

  

第七种是"mime/quotedprintable"包实现的io.Writer接口。

// Write encodes p using quoted-printable encoding and writes it to the

// underlying io.Writer. It limits line length to 76 characters. The encoded

// bytes are not necessarily flushed until the Writer is closed.

func (w *Writer) Write(p []byte) (n int, err error) {

  

第八种也是比较常用的,由log包提供的。



这么多种Hello World的写法可能不是全面的,但这是我见过的写法最多的一种语言。

Golang丰富的I/O----用N种Hello World展示

标签:ble   --   感受   定义   col   ati   ted   count   turn   

原文地址:http://www.cnblogs.com/majianguo/p/7911737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!