zoukankan      html  css  js  c++  java
  • Golang丰富的I/O----用N种Hello World展示

    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!
    ")
    
        os.Stdout.WriteString("Hello, World!
    ")
    
    
        //3
    
        w := bufio.NewWriter(os.Stdout)
    
        fmt.Fprint(w, "Hello, ")
    
        fmt.Fprint(w, "world!
    ")
    
        w.Flush() // Don't forget to flush!
    
    
        fmt.Fprint(os.Stdout, "hello, world!
    ")
    
    
        //4
    
        r := strings.NewReader("hello, world!
    ")
    
        if _, err := io.Copy(os.Stdout, r); err != nil {
    
            log.Fatal(err)
    
        }
    
    
        r1 := strings.NewReader("hello, world!
    ")
    
        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!
    ")
    
        //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!
    ")
    
        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!
    "))
    
    
        //7
    
        wQuote := quotedprintable.NewWriter(os.Stdout)
    
        wQuote.Write([]byte("Hello, world!
    "))
    
    
        wQuote.Write([]byte("These symbols will be escaped: = 	"))
    
        wQuote.Close()
    
        wQuote.Write([]byte("
    "))
    
    
        //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的写法可能不是全面的,但这是我见过的写法最多的一种语言。

  • 相关阅读:
    Day 10 网络基础
    Day9 编码和网络基础 Encoding and network
    Day 8 数据与进制转换
    Day 7 文件管理补充和练习 File management & exercises
    5--kubernetes实战初入门
    4--k8s之资源管理 ; yaml语言
    3--二进制安装k8s
    kubernetes之安装集群图形化界面Dadhboard
    2--kubeadm安装k8s
    1--kubernetes简介及架构图
  • 原文地址:https://www.cnblogs.com/majianguo/p/7911737.html
Copyright © 2011-2022 走看看