go语言标准库
2014 年 11 月 7 日
像 fmt
、 os
等这样具有常用功能的内置包在 Go 语言中有 150 个以上,它们被称为标准库,大部分(一些底层的除外)内置于 Go 本身。完整列表可以在 Go Walker
查看。
-
unsafe
: 包含了一些打破 Go 语言“类型安全”的命令,一般的程序中不会被使用,可用在 C/C++ 程序的调用中。 -
syscall
–os
–os/exec
:os os/exec syscall
通过一个 Go 程序让Linux重启来体现它的能力。
package main import ( "syscall" ) const LINUX_REBOOT_MAGIC1 uintptr = 0xfee1dead const LINUX_REBOOT_MAGIC2 uintptr = 672274793 const LINUX_REBOOT_CMD_RESTART uintptr = 0x1234567 func main() { syscall.Syscall(syscall.SYS_REBOOT, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART) } 复制代码
-
archive/tar
和/zip-compress
:压缩(解压缩)文件功能。 -
fmt
–io
–bufio
–path/filepath
–flag
:fmt io bufio path/filepath flag
-
strings
–strconv
–unicode
–regexp
–bytes
:strings strconv unicode regexp bytes index/suffixarray
-
math
–math/cmath
–math/big
–math/rand
–sort
:math math/cmath math/rand sort math/big
-
container
–/list-ring-heap
: 实现对集合的操作。list ring
下面代码演示了如何遍历一个链表(当 l 是 *List
):
for e := l.Front(); e != nil; e = e.Next() { //do something with e.Value } 复制代码
-
time
–log
:time log
-
encoding/json
–encoding/xml
–text/template
:encoding/json encoding/xml text/template
-
net
–net/http
–html
:net http html
-
runtime
: Go 程序运行时的交互操作,例如垃圾回收和协程创建。 -
reflect
: 实现通过程序运行时反射,让程序操作任意类型的变量。
exp
包中有许多将被编译为新包的实验性的包。它们将成为独立的包在下次稳定版本发布的时候。如果前一个版本已经存在了,它们将被作为过时的包被回收。然而 Go1.0 发布的时候并不包含过时或者实验性的包。