~~ Use standard library
1. Avoid reinventing the wheel
The official provides a lot of libraries for us to use, which are packaged wheels, such as bags fmt
, and we use it many times to print data.
We can view the implementation inside:
package fmt
func Println(a ...interface{}) (n int, err error) {
return Fprintln(os.Stdout, a...)
}
func Printf(format string, a ...interface{}) (n int, err error) {
return Fprintf(os.Stdout, format, a...)
}
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
p := newPrinter()
p.doPrintf(format, a)
n, err = w.Write(p.buf)
p.free()
return
}
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
p := newPrinter()
p.doPrintln(a)
n, err = w.Write(p.buf)
p.free()
return
}
The function Println
is to directly print and wrap, and Printf
the function is to format the output, such as:
// 打印一行空行
fmt.Println()
// 打印 4 5 6
fmt.Println(4, 5, 6)
// 占位符 %d 打印数字,\n换行
fmt.Printf("%d\n", 2)
// 占位符 %s 打印字符串,\n换行
fmt.Printf("%s\n", "cat")
// 占位符 %v或者%#v 打印任何类型,\n换行
fmt.Printf("%#v,%v\n", "cat", 33)
// 更多示例
fmt.Printf("%s,%d,%s,%v,%#v\n", "cat", 2, "3", map[int]string{1: "s"}, map[int]string{1: "s"})
Output:
4 5 6
2
cat
"cat",33
cat,2,3,map[int]string{1:"s"}
The function Printf
uses another function Fprintf
, and Fprintf
other structure methods are called inside the function .
For what we often use func Printf(format string, a ...interface{})
, we pass in format
and many variables a ...interface{}
, and we can print the results we want in the console. Such as:
fmt.Printf("%s,%d,%s,%v,%#v\n", "cat", 2, "3", map[int]string{1: "s"}, map[int]string{1: "s"})
Among them %
is a placeholder, which means that the following variables take place one by one. The lowercase letters after the %s
placeholders %d
indicate the type of placeholder, the string placeholder, the numeric type placeholder, %v
or the %#v
unknown type placeholder. It will automatically determine the type and print, plus it #
will print a bit more detailed. Because the print will not wrap, we need to use the \n
newline character to wrap.
At some point, we can use official libraries or libraries written by others, after all, wheel rebuilding takes time.
At the same time, if you want to improve the development speed IDE
, it is recommended to install , that is Integrated Development Environment
(integrated development environment), such as Goland
(native support Golang
) or IDEA
software (plug-in installation required).
2. Summary
We only learned Golang
a subset of the language. If you want to learn more in detail, you can docker
open the terminal after installation :
# 拉镜像
docker pull hunterhug/gotourzh
# 后台运行
docker run -d -p 9999:9999 hunterhug/gotourzh
Open the browser and enter: 127.0.0.1:9999 to learn more comprehensively.
The latter algorithm analysis and implementation will be used Golang
as an example.
Series article entry
I am the star Chen, Welcome I have personally written data structures and algorithms (Golang achieve) , starting in the article to read more friendly GitBook .
- Data structure and algorithm (Golang implementation) (1) A simple introduction to Golang-Preface
- Data structures and algorithms (Golang implementation) (2) A simple introduction to Golang-packages, variables and functions
- Data structure and algorithm (Golang implementation) (3) A simple introduction to Golang-flow control statement
- Data structures and algorithms (Golang implementation) (4) A simple introduction to Golang-structures and methods
- Data structure and algorithm (Golang implementation) (5) A simple introduction to Golang-interface
- Data structure and algorithm (Golang implementation) (6) A simple introduction to Golang-concurrency, coroutines and channels
- Data structure and algorithm (Golang implementation) (7) A simple introduction to Golang-standard library
- Data Structure and Algorithm (Golang Implementation) (8.1) Basic Knowledge-Preface
- Data Structure and Algorithm (Golang Implementation) (8.2) Basic Knowledge-Divide and Conquer and Recursion
- Data structure and algorithm (Golang implementation) (9) Basic knowledge-algorithm complexity and progressive symbol
- Data structure and algorithm (Golang implementation) (10) Basic knowledge-the main method of algorithm complexity
- Data Structures and Algorithms (Golang Implementation) (11) Common Data Structures-Preface
- Data structures and algorithms (Golang implementation) (12) Common data structures-linked lists
- Data structures and algorithms (Golang implementation) (13) Common data structures-variable-length arrays
- Data structures and algorithms (Golang implementation) (14) Common data structures-stack and queue
- Data Structures and Algorithms (Golang Implementation) (15) Common Data Structures-List
- Data Structures and Algorithms (Golang Implementation) (16) Common Data Structures-Dictionary
- Data structures and algorithms (Golang implementation) (17) Common data structures-trees
- Data Structure and Algorithm (Golang Implementation) (18) Sorting Algorithm-Preface
- Data structure and algorithm (Golang implementation) (19) Sorting algorithm-bubble sorting
- Data structure and algorithm (Golang implementation) (20) Sorting algorithm-selection sorting
- Data structure and algorithm (Golang implementation) (21) Sorting algorithm-insertion sort
- Data structure and algorithm (Golang implementation) (22) Sorting algorithm-Hill sorting
- Data structure and algorithm (Golang implementation) (23) Sorting algorithm-merge sorting
- Data structure and algorithm (Golang implementation) (24) Sorting algorithm-priority queue and heap sorting
- Data structure and algorithm (Golang implementation) (25) Sorting algorithm-quick sorting
- Data Structure and Algorithm (Golang Implementation) (26) Lookup Algorithm-Hash Table
- Data structure and algorithm (Golang implementation) (27) Search algorithm-binary search tree
- Data structure and algorithm (Golang implementation) (28) Search algorithm-AVL tree
- Data structure and algorithm (Golang implementation) (29) Finding algorithm-2-3 tree and left-leaning red-black tree
- Data structure and algorithm (implemented by Golang) (30) Finding algorithm-2-3-4 tree and ordinary red-black tree