Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read interface interface from go source code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to read the interface interface from the go source code, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Interfaces in go source code

In the http library, the get method:

one

Func Get (url string) (resp * Response, err error)

Response:

one

two

three

four

five

six

seven

eight

nine

Type Response struct {

Status string / / e.g.200 OK

StatusCode int / / e.g. 200

Proto string / / e.g. "HTTP/1.0"

ProtoMajor int / / e.g. 1

ProtoMinor int / / e.g. 0

Header Header

Body io.ReadCloser

Io.ReadCloser:

one

two

three

four

Type ReadCloser interface {

Reader

Closer

}

Reader:

one

two

three

Type Reader interface {

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

}

Closer:

one

two

three

Type Closer interface {

Close () error

}

Whether it is reading files, network and other operations, the same Reader interface is implemented. When the [] byte is passed in, the read byte is placed in. Success returns the number of successes.

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

Package main

Import (

"net/http"

"fmt"

"os"

)

Func main () {

Resp,err: = http.Get ("http://tmall.com")

If err! = nil {

Fmt.Println ("Error:", err)

Os.Exit (1)

}

/ / if the setting is larger, the read method will not automatically expand the capacity.

Bs:=make ([] byte,99999)

The / / read function puts the read data into the bs.

Resp.Body.Read (bs)

Fmt.Println (string (bs))

}

Write interface 1

two

three

Type Writer interface {

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

}

Io.opy1

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

Package main

Import (

"net/http"

"fmt"

"os"

"io"

)

Func main () {

Resp,err: = http.Get ("http://tmall.com")

If err! = nil {

Fmt.Println ("Error:", err)

Os.Exit (1)

}

/ / read the information in resp.Body and write it to os.Stdout. Os.Stdout implements the write interface and resp.Body implements the read interface.

Io.Copy (os.Stdout,resp.Body)

}

Io.copy source code 1

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

thirty-four

thirty-five

thirty-six

thirty-seven

thirty-eight

thirty-nine

forty

forty-one

forty-two

forty-three

forty-four

forty-five

forty-six

forty-seven

forty-eight

forty-nine

fifty

fifty-one

fifty-two

fifty-three

fifty-four

fifty-five

fifty-six

fifty-seven

fifty-eight

Func Copy (dst Writer, src Reader) (written int64, err error) {

Return copyBuffer (dst, src, nil)

}

-

Func CopyBuffer (dst Writer, src Reader, buf [] byte) (written int64, err error) {

If buf! = nil & & len (buf) = 0 {

Panic ("empty buffer in io.CopyBuffer")

}

Return copyBuffer (dst, src, buf)

}

-

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

/ / If the reader has a WriteTo method, use it to do the copy.

/ / Avoids an allocation and a copy.

If wt, ok: = src. (WriterTo); ok {

Return wt.WriteTo (dst)

}

/ / Similarly, if the writer has a ReadFrom method, use it to do the copy.

If rt, ok: = dst. (ReaderFrom); ok {

Return rt.ReadFrom (src)

}

Size: = 32 * 1024

If l, ok: = src. (* LimitedReader); ok & & int64 (size) > l.N {

If l.N

< 1 { size = 1 } else { size = int(l.N) } } if buf == nil { buf = make([]byte, size)//分配 } for { //疯狂读取并写入 nr, er := src.Read(buf) if nr >

0 {

Nw, ew: = dst.Write (buf [0: nr])

If nw > 0 {

Written + = int64 (nw)

}

If ew! = nil {

Err = ew

Break

}

If nr! = nw {

Err = ErrShortWrite

Break

}

}

If er! = nil {

If er! = EOF {

Err = er

}

Break

}

}

Return written, err

}

Custom Writer function 1

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

Package main

Import (

"net/http"

"fmt"

"os"

"io"

)

Type logWriter struct {}

Func main () {

Resp,err: = http.Get ("http://tmall.com")

If err! = nil {

Fmt.Println ("Error:", err)

Os.Exit (1)

}

Lw:= logWriter {}

/ / read the information in resp.Body and write it to os.Stdout. Os.Stdout implements the write interface and resp.Body implements the read interface.

Io.Copy (lw,resp.Body)

}

Func (logWriter) Write (bs [] byte) (int,error) {

Fmt.Println (string (bs))

Fmt.Println ("Just wrote this many bytes", len (bs))

Return len (bs), nil

}

The above content is how to read the interface interface from the go source code. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report