Data structure and algorithm (Golang implementation) (7) A simple introduction to Golang-standard library

~~ 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 Printlnis to directly print and wrap, and Printfthe 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 Printfuses another function Fprintf, and Fprintfother structure methods are called inside the function .

For what we often use func Printf(format string, a ...interface{}), we pass in formatand 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 %splaceholders %dindicate the type of placeholder, the string placeholder, the numeric type placeholder, %vor the %#vunknown 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 \nnewline 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 IDEAsoftware (plug-in installation required).

2. Summary

We only learned Golanga subset of the language. If you want to learn more in detail, you can dockeropen 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 Golangas 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 .

Published 12 original articles · praised 0 · visits 92

Guess you like

Origin blog.csdn.net/m0_46803965/article/details/105595033