This is a creation in Article, where the information may have evolved or changed.
1. Pointers to Golang
can be unsafe. Pointer convert to UIntPtr, then perform pointer operations in disguise
Package Mainimport "FMT" import "unsafe" type User struct {Id intname string}func Main () {u: = &user{1, ' Tom '}var up UIntPtr = uintptr (unsafe. Pointer (U)) + unsafe. Offsetof (U.name) var Name *string = (*string) (unsafe. Pointer (UP)) fmt. Println (*name)}
2. Package IO
Implementation of IO. Type of reader or Io.writer interface (type of export):
- Os. The File also implements IO. Reader and Io.writer
- Strings. The Reader implements IO. Reader
- Bufio. The Reader/writer implements IO separately. Reader and Io.writer
- bytes. The Buffer also implements IO. Reader and Io.writer
- bytes. The Reader implements IO. Reader
- Compress/gzip. The Reader/writer implements IO separately. Reader and Io.writer
- Crypto/cipher. The Streamreader/streamwriter implements IO separately. Reader and Io.writer
- Crypto/tls. The Conn also implements IO. Reader and Io.writer
- Encoding/csv. The Reader/writer implements IO separately. Reader and Io.writer
- Mime/multipart. The part implements IO. Reader
Among the above types, the commonly used types are: OS. File, Strings. Reader, Bufio. Reader/writer, Bytes. Buffer, Bytes. Reader
A simple example of the Writeat () method:
File, err: = OS. Create ("WriteAt.txt") if err! = Nil { panic (err)}defer file. Close () file. WriteString ("Golang Chinese community--here is superfluous") n, Err: = file. Writeat ([]byte ("Go Language Learning Corner"), if err! = Nil { panic (err)}fmt. PRINTLN (N)
File. WriteString ("Golang Chinese community-here is superfluous") writes Golang Chinese community to a file -superfluous here; Writeat ([]byte ("Go Language Learning Corner") writes to the Go language learning corner at the offset=24 of the file stream (which overwrites the contents of that location).
A simple example of the Readfrom () method:
File, err: = OS. Open ("WriteAt.txt") if err! = Nil { panic (err)}defer file. Close () Writer: = Bufio. Newwriter (OS. Stdout) writer. Readfrom (file) writer. Flush ()
WriteTo () Method Example:
Reader: = bytes. Newreader ([]byte ("Go Language Learning Corner") reader. WriteTo (OS. Stdout)
The Seek () Method Example:
Reader: = strings. Newreader ("Go Language Learning Corner") reader. Seek ( -6, OS. Seek_end) R, _, _: = Reader. Readrune () fmt. Printf ("%c\n", R)
In the standard library, IO is implemented with the following types. Bytereader or Io.bytewriter:
- Bufio. The Reader/writer implements IO separately. Bytereader and Io.bytewriter
- bytes. The Buffer also implements IO. Bytereader and Io.bytewriter
- bytes. The Reader implements IO. Bytereader
- Strings. The Reader implements IO. Bytereader
In the next example, we pass the bytes. Buffer to read or write one byte at a time (main code):
var ch bytefmt. SCANF ("%c\n", &ch) Buffer: = new (bytes. Buffer) Err: = buffer. WriteByte (ch) if Err = = Nil { FMT. Println ("Write a byte successfully!") Ready to read this byte ... ") Newch, _: = buffer. ReadByte () FMT. Printf ("bytes read:%c\n", Newch)} else { fmt. Println ("Write Error")}
Examples of Pipe () methods and Pipereader, Pipewriter structures:
Func Main () { pipe ()}func pipe () { Pipereader, pipewriter: = Io. Pipe () go pipewrite (pipewriter) go piperead (pipereader) time . Sleep (1e7)}func pipewrite (Pipewriter *io. Pipewriter) { var ( i = 0 err error n int ) data: = []byte ("Go Language Learning Corner") for _, err = Pi Pewriter.write (data); Err = = Nil; N, err = pipewriter.write (data) { i++ if i = = 3 { pipewriter.closewitherror (errors. New ("Output 3 times after End") } } fmt. Println ("The number of bytes output after close:", N, "error:", Err)}func piperead (Pipereader *io. Pipereader) { var ( err error n int ) data: = Make ([]byte, 1024x768) for n, err = Pipereader.read (data); Err = = Nil; N, err = pipereader.read (data) { FMT. Printf ("%s\n", Data[:n]) } fmt. Println ("After writer end Closewitherror:", Err)}
Example Copy () method:
Package Mainimport ( "FMT" "io" " OS") Func main () { io. Copy (OS. Stdout, Strings. Newreader ("Go Language Learning Corner") io. Copy (OS. Stdout, OS. Stdin) FMT. Println ("Got EOF--Bye")}
3. Program Structure
Suppose there is a general implementation of a service like this:
Type Service struct {wg sync. Waitgroup}func (P *service) Start () {p.wg.add (1) Go P.run ()}func (P *service) Close () {//... p.wg.wait ()}func (P *service) Run () {defer p.wg.done ()//...}
where run () is the primary logical implementation of the service. The problem with this writing is that the WG's add and done function calls are separated, and it is easy to inadvertently cause the add and do calls to not match in subsequent code maintenance so that the results of wait execution do not meet expectations. From the way the code is organized, it is important to put the code for the function as close together as possible.
Type Service struct {wg sync. Waitgroup}func (P *service) Start () {p.wg.add (1) go func () {P.run () P.wg.done ()} ()}func (P *service) Close () {//... P.wg.wa It ()}func (P *service) run () {//...}
This is actually common sense, and people who have written some code may know it. But in the actual code, we often find some kind of bad writing. So, for these idioms (or upgrading to code specifications), I think it's more important not to know, but to implement and obey.