GO - 标准库

go - 标准库

标准库参考地址

bash 复制代码
https://golang.google.cn/pkg/

标准库说明

数据处理

encoding/(例如:encoding/json、encoding/xml、encoding/base64)
hash/
(例如:hash/adler32、hash/crc32、hash/fnv)

bytes

strconv
文件操作

os

path/(例如:path/filepath)
bufio
compress/
(例如:compress/gzip、compress/zlib)
网络通信

net

net/http

net/http/(例如:net/http/httputil、net/http/pprof)
net/url
net/rpc
net/smtp
加密和安全
crypto/
(例如:crypto/md5、crypto/aes、crypto/rsa)

crypto/rand

crypto/tls

crypto/x509
时间处理

time
容器和集合

container/(例如:container/heap、container/list)
sort
并发和同步
sync/
(例如:sync/atomic)

runtime

runtime/pprof

runtime/trace
测试和调试

testing

testing/(例如:testing/iotest、testing/quick)
文本处理
fmt
strings
unicode/
(例如:unicode/utf8、unicode/utf16)

text/(例如:text/template、text/scanner)
其他
errors
flag
log
math
math/
(例如:math/big、math/rand)

sync/atomic

unsafe

示例

文件操作示例(os、path/filepath、bufio)

go 复制代码
package main

import (
    "os"
    "path/filepath"
    "bufio"
)

func main() {
    // 创建文件
    file, err := os.Create("example.txt")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // 写入文件
    writer := bufio.NewWriter(file)
    _, err = writer.WriteString("Hello, World!\n")
    if err != nil {
        panic(err)
    }
    writer.Flush()

    // 读取目录
    files, err := filepath.Glob("*.txt")
    if err != nil {
        panic(err)
    }
    for _, f := range files {
        println(f)
    }
}

网络通信示例(net/http、net/url)

go 复制代码
package main

import (
    "net/http"
    "net/url"
)

func main() {
    // 发送 HTTP 请求
    resp, err := http.Get("https://www.example.com")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    // 解析 URL
    u, err := url.Parse("https://www.example.com/path?query=value")
    if err != nil {
        panic(err)
    }
    println("Host:", u.Host)
    println("Path:", u.Path)
    println("Query:", u.Query().Get("query"))
}

并发和同步示例(sync)

go 复制代码
package main

import (
    "sync"
    "time"
)

func main() {
    var wg sync.WaitGroup
    wg.Add(2)

    go func() {
        defer wg.Done()
        time.Sleep(time.Second)
        println("Goroutine 1 completed")
    }()

    go func() {
        defer wg.Done()
        time.Sleep(2 * time.Second)
        println("Goroutine 2 completed")
    }()

    wg.Wait()
    println("All goroutines completed")
}
相关推荐
极客代码15 分钟前
深入解析C语言中的函数指针:原理、规则与实践
c语言·开发语言·指针·状态机·函数·函数指针
w-w0w-w36 分钟前
C++模板参数与特化全解析
开发语言·c++
不绝19140 分钟前
C#核心:继承
开发语言·c#
AI即插即用2 小时前
即插即用系列(代码实践)专栏介绍
开发语言·人工智能·深度学习·计算机视觉
码农水水2 小时前
蚂蚁Java面试被问:混沌工程在分布式系统中的应用
java·linux·开发语言·面试·职场和发展·php
喵了meme2 小时前
c语言经验分享
c语言·开发语言
晚风吹长发2 小时前
初步了解Linux中的动静态库及其制作和使用
linux·运维·服务器·数据结构·c++·后端·算法
Knight_AL2 小时前
用 JOL 验证 synchronized 的锁升级过程(偏向锁 → 轻量级锁 → 重量级锁)
开发语言·jvm·c#
啊阿狸不会拉杆2 小时前
《数字图像处理》第 4 章 - 频域滤波
开发语言·python·数字信号处理·数字图像处理·频率域滤波