Skip to content

标准库

Golang 的标准库非常丰富,涵盖了很多常用的功能模块。以下是 Golang 标准库中的主要模块分类及部分示例:

  • fmt:格式化 I/O

    import "fmt"
    fmt.Println("Hello, World!")
  • errors:创建和操作错误

    import "errors"
    var err = errors.New("error message")
  • log:简单的日志服务

    import "log"
    log.Println("This is a log message")
  • math:基本数学函数

    import "math"
    result := math.Sqrt(16)
  • math/rand:伪随机数生成

    import "math/rand"
    num := rand.Intn(100)
  • strings:字符串操作

    import "strings"
    s := strings.ToUpper("hello")
  • strconv:字符串与基本数据类型的转换

    import "strconv"
    num, err := strconv.Atoi("123")
  • time:时间操作

    import "time"
    now := time.Now()
  • io:基本 I/O 原语

    import "io"
  • io/ioutil:I/O 工具函数

    import "io/ioutil"
    data, err := ioutil.ReadFile("file.txt")
  • os:操作系统功能

    import "os"
    file, err := os.Open("file.txt")
  • path:路径操作

    import "path"
    p := path.Join("dir", "file.txt")
  • path/filepath:文件路径操作

    import "path/filepath"
    p, err := filepath.Abs("file.txt")
  • net:基本网络操作

    import "net"
    conn, err := net.Dial("tcp", "google.com:80")
  • net/http:HTTP 客户端和服务器

    import "net/http"
    resp, err := http.Get("http://example.com")
  • container/list:双向链表

    import "container/list"
    l := list.New()
    l.PushBack("element")
  • container/ring:环形链表

    import "container/ring"
    r := ring.New(5)
  • compress/gzip:gzip 压缩

    import "compress/gzip"
  • crypto:通用加密包

    import "crypto"
  • crypto/md5:MD5 哈希算法

    import "crypto/md5"
    hash := md5.Sum([]byte("data"))
  • crypto/sha256:SHA256 哈希算法

    import "crypto/sha256"
    hash := sha256.Sum256([]byte("data"))
  • sync:基本同步原语

    import "sync"
    var mu sync.Mutex
  • sync/atomic:低级原子操作

    import "sync/atomic"
    var counter int64
    atomic.AddInt64(&counter, 1)
  • reflect:运行时反射

    import "reflect"
    t := reflect.TypeOf(123)
  • encoding/json:JSON 编码和解码

    import "encoding/json"
    jsonStr, _ := json.Marshal(map[string]string{"hello": "world"})
  • encoding/xml:XML 编码和解码

    import "encoding/xml"
  • testing:测试框架

    import "testing"
  • database/sql:SQL 数据库

    import "database/sql"
    import _ "github.com/go-sql-driver/mysql"
  • flag:命令行参数解析

    import "flag"
    name := flag.String("name", "default", "name description")
    flag.Parse()
  • regexp:正则表达式

    import "regexp"
    re := regexp.MustCompile(`\d+`)

| 分类 | 包名称 | 功能描述 | |----------------|-----------------------|--------------------------------| | 基础包 | fmt , errors , log| 格式化 I/O、错误处理、日志服务 | | 数学包 | math , math/rand | 数学函数、伪随机数生成 | | 字符串处理包 | strings , strconv | 字符串操作、类型转换 | | 时间处理包 | time | 时间操作 | | I/O 包 | io , io/ioutil , os| 基本 I/O 操作、文件系统操作 | | 文件系统包 | path , path/filepath| 路径操作、文件路径操作 | | 网络包 | net , net/http | 网络操作、HTTP 客户端和服务器 | | 数据结构包 | container/list , container/ring| 数据结构 | | 压缩包 | compress/gzip | gzip 压缩 | | 加密包 | crypto , crypto/md5 , crypto/sha256| 加密和哈希算法 | | 并发包 | sync , sync/atomic | 并发和同步原语 | | 反射包 | reflect | 运行时反射 | | 编码和解码包 | encoding/json , encoding/xml| JSON 和 XML 编码解码 | | 测试包 | testing | 测试框架 | | 数据库包 | database/sql | SQL 数据库操作 | | 其他常用包 | flag , regexp | 命令行解析、正则表达式 |