Start reading the Go source code
It’s been a while since you’ve learned the basics of Go, so what should you learn next? There are several directions to consider, such as Web development, network programming, etc.
Before the next stage of study, I wrote an open source project | Go to develop a distributed unique ID generation system. If you are interested in this project, you can get the source code on GitHub.
In the process of writing the project, a problem was discovered. It’s ok to implement functionality, but you don’t know if your code is going to be Go style or elegant. So INSTEAD of continuing to learn about application development, I thought it would be better to get down to the bottom and lay the groundwork for writing Go code.
So, I decided to start reading the Go standard library source code, Go has a total of 150+ standard library, if you want to read all of it is not impossible, but it is definitely a big project, I hope I can stick to it.
Why start with the Go library source code? Because I have read some books on the basic principles of Go recently, to be honest, such as Goroutine scheduling, GC garbage collection, I can’t understand them at all. If you read this part of the code, I am afraid to give up the Go language learning.
The standard library, on the other hand, has a part of the code that doesn’t involve the underlying principles at all, is relatively simple to implement, and at the same time gives you a deeper understanding of the concept of Go, which is a great way to get started. Then from simple to deep, step by step, like fighting monsters upgrade, step by step to conquer Go.
So how do you read it? I came up with some ideas:
- Look at the official standard library documentation.
- Read other people’s technical articles online.
- Write some examples to practice using them.
- If you can, implement the library functionality yourself.
- Summarize your reading experience and output it.
You can use one or more of the above methods in combination, and then continue to read and summarize, and finally find a method that suits you completely.
Here are some of the standard libraries and their features:
archive/tar
和/zip-compress
: Compresses (uncompresses) files.fmt
–io
–bufio
–path/filepath
–flag
:fmt
: Provides the input and output formatting function.io
: Provides basic I/O functions, mostly wrapped around system functions.bufio
: Encapsulation of buffering I/O functions.path/filepath
: used to operate on the path of the target file name in the current system.flag
: Provides operations on command line arguments.
strings
–strconv
–unicode
–regexp
–bytes
:strings
: Provides operations on strings.strconv
: provides the ability to convert a string to an underlying type.unicode
: Provides special functionality for Unicode strings.regexp
: Regular expression function.bytes
: Provides operations on character sharding.index/suffixarray
: Quick query of substrings.
math
–math/cmath
–math/big
–math/rand-sort
:math
: Basic mathematical functions.math/cmath
: Operations on complex numbers.math/rand
: pseudo-random number generation.sort
: Sort and customize collections for arrays.math/big
: Realization and calculation of large numbers.
container
–/list
–/ring
–/heap
:list
: doubly linked list.ring
: circular linked list.heap
: the heap.
compress/bzip2
–/flate
–/gzip
–/lzw
–zlib
:compress/bzip2
: Decompresses bzip2.flate
: Implements deflate data compression format, as described in RFC 1951.gzip
: Enables reading and writing of gzip compressed files.lzw
: Lempel Ziv Welch Compression data format implementation.zlib
: To achieve zlib data compression format read and write.
context
: To simplify operations related to data, cancellation signals, expiration times, etc. in the request domain between multiple Goroutines handling a single request.crypto
–crypto/md5
–crypto/sha1
:crypto
: A set of common password constants.crypto/md5
: indicates MD5 encryption.crypto/sha1
: SHA1 encryption.
errors
: Implement the method of operation error.expvar
: Provides a standardized interface for public variables.hash
: common interface to all hash function implementations.html
: HTML text transcoding escape function.sort
: provides raw functions to sort slices and user-defined collections.unsafe
: contains some commands that break the “type safety” of the Go language, which are not used by general programs and can be used in C/C++ program calls.syscall
–os
–os/exec
:syscall
: provides a basic interface for low-level calls of the operating system.os
: gives us a platform-independent operating system functional interface with a Unix-like design that hides differences between operating systems and makes different file systems and operating system objects behave consistently.os/exec
: provides a way to run external operating system commands and programs.
time
–log
:time
: Basic operation of date and time.log
: Records logs generated when the program is running.
encoding/json
–encoding/xml
–text/template
:encoding/json
: Reads and decodes and writes and encodes JSON data.encoding/xml
: simple XML1.0 parser.text/template
: Generates htML-like data-driven templates that mix data and text.
net
–net/http
:net
: Basic operations on network data.http
: provides an extensible HTTP server and base client that parses HTTP requests and replies.
runtime
: Go program runtime interactions, such as garbage collection and coroutine creation.reflect
: implements runtime reflection through the program, allowing the program to operate on variables of any type.
This is just a partial list of standard libraries, but for a more comprehensive list you can go to the official website.
So where do all these libraries start?
Here I make a simple classification, due to the limited level, can only do some simple sorting, and then you can combine their own actual situation to make a choice.
Some libraries involve very specialized knowledge and the input-output ratio may be low. For example, Archive, COMPRESS, and crypto involve knowledge of compression and encryption algorithms.
Some libraries belong to utility classes, such as Bufio, Bytes, Strings, Path, Strconv, and so on. These libraries have no domain knowledge and are easy to read.
Some libraries work with operating systems, such as OS, NET, sync, etc. Learning these libraries requires a clear understanding of the operating system.
Many subpackets under NET are related to network protocols, such as NET/HTTP, which involves the parsing of HTTP packets. Therefore, you need to be familiar with network protocols.
If you want to understand the underlying principles of the language, you need to read the Runtime library.
To get started quickly and understand the design concepts of the language, I recommend reading IO and FMT libraries to better understand interface design.
I’ve looked at some of the source code, and it’s painful, but really useful. It may be difficult and take a long time to understand in the early stage, but after the set routine is formed, you will become more and more familiar with it, spend less time and understand it more deeply.
I will continue to summarize the output, please continue to pay attention to, let’s learn.
Open Source projects:
- Github.com/yongxinz/id…
Previous articles:
- Open source project | Go development of a distributed unique ID generation system
- Test little sister asked me how to use gRPC, I directly threw this article to her
- GRPC, critical praise