标准库
Golang的标准库非常丰富,涵盖了很多常用的功能模块。以下是Golang标准库中的主要模块分类及部分示例:
fmt:格式化I/Oimport "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)
字符串处理包
Section titled “字符串处理包”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.Mutexsync/atomic:低级原子操作import "sync/atomic"var counter int64atomic.AddInt64(&counter, 1)
reflect:运行时反射import "reflect"t := reflect.TypeOf(123)
编码和解码包
Section titled “编码和解码包”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+`)
标准库模块总结
Section titled “标准库模块总结”| 分类 | 包名称 | 功能描述 |
|---|---|---|
| 基础包 | 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 | 命令行解析、正则表达式 |