今天我们完结了这个strings的包,其实我们就剩下了type Reader和type Replacer这个我们之间讲过io的包,这样大家理解起来就比较省劲了!

func NewReader(s string) *Reader
import (
    "fmt"
    "reflect"
    "strings")func main() {
    reader := strings.NewReader("widuu web")
    fmt.Println(reflect.TypeOf(reader)) //*strings.Reader
    fmt.Println(reader.Len())           //9}
func (r *Reader) Len() int
func (r *Reader) Read(b []byte) (n int, err error)
import (
    "fmt"
    "strings")func main() {
    reader := strings.NewReader("hello widuu web")
    n, err := reader.Read(make([]byte, 10))
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(n) //10}
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
import (
    "fmt"
    "strings")func main() {
    reader := strings.NewReader("hello widuu web")
    b := make([]byte, 10)
    n, err := reader.ReadAt(b, 2)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(b[:n])) //llo widuu}
func (r *Reader) ReadByte() (b byte, err error)
import (
    "fmt"
    "strings")func main() {
    reader := strings.NewReader("hello widuu web")
    data, err := reader.ReadByte()
    if err == nil {
        fmt.Println(string(data))
        return
    }
    fmt.Println(err) //返回一个字节h}
func (r *Reader) ReadRune() (ch rune, size int, err error)
import (
    "fmt"
    "strings")func main() {
    reader := strings.NewReader("hello widuu web")
    r, n, err := reader.ReadRune()
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(string(r), n) //h,1}
func (r *Reader) Seek(offset int64, whence int) (int64, error)
import (
    "fmt"
    "strings")func main() {
    reader := strings.NewReader("hello widuu")
    b := make([]byte, 8)
    n, _ := reader.Read(b)
    fmt.Println(string(b[:n])) //hello wi 这个时候没有读到要读d
    reader.Seek(2, 1)          //这个时候我们在读取位置 偏移2
    n1, _ := reader.Read(b)
    fmt.Println(string(b[:n1])) //u}
func (r *Reader) UnreadByte() error
import (
    "fmt"
    "strings")func main() {
    reader := strings.NewReader("hello widuu")
    b := make([]byte, 8)
    n, _ := reader.Read(b)
    fmt.Println(string(b[:n])) //hello wi 这个时候没有读到要读d
    reader.Seek(2, 1)          //这个时候我们在读取位置 偏移2
    reader.UnreadByte()        //然后向前偏移1字节
    n1, _ := reader.Read(b)
    fmt.Println(string(b[:n1])) //uu}
func (r *Reader) UnreadRune() error
func (r *Reader) WriteTo(w io.Writer) (n int64, err error)
import (
    "fmt"
    "os"
    "strings")func main() {
    reader := strings.NewReader("hello widuu")
    w, _ := os.Create("test.txt")
    defer w.Close()
    n, err := reader.WriteTo(w)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(n) //11}
func NewReplacer(oldnew ...string) *Replacer NewReplacer
import (
    "fmt"
    "strings")func main() {
    r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
    fmt.Println(r.Replace("This is <b>HTML</b>!")) //This is &lt;b&gt;HTML&lt;/b&gt;!}
func (r *Replacer) Replace(s string) string
func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)
import (
    "fmt"
    "io/ioutil"
    "os"
    "strings")func main() {
    r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
    w, _ := os.OpenFile("test1.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
    defer w.Close()
    n, err := r.WriteString(w, "This is <b>HTML</b>!")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(n)
    d, _ := ioutil.ReadFile("test1.txt") //32
    fmt.Println(string(d))               //This is &lt;b&gt;HTML&lt;/b&gt;!}

每天只讲一点golang的标准库,方便大家学习和使用,更多的时候去理解标准库,大家多动手,如果你喜欢请继续关注我们

github https://github.com/widuu/gopkg