GO - 标准库

go - 标准库

标准库参考地址

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)

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)

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)

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")
}

相关推荐

  1. GO - 标准

    2024-04-08 16:48:01       36 阅读
  2. go标准Context上下文

    2024-04-08 16:48:01       48 阅读
  3. go标准---net/http服务端

    2024-04-08 16:48:01       21 阅读
  4. go标准和第三方使用

    2024-04-08 16:48:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-08 16:48:01       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 16:48:01       97 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 16:48:01       78 阅读
  4. Python语言-面向对象

    2024-04-08 16:48:01       88 阅读

热门阅读

  1. Hamilton-Jacobi-Bellman (HJB) 方程

    2024-04-08 16:48:01       40 阅读
  2. 第十四届蓝桥杯省赛大学B组填空题(c++)

    2024-04-08 16:48:01       40 阅读
  3. Android Apk签名算法使用SHA256

    2024-04-08 16:48:01       36 阅读
  4. C++ 动态字符串String的介绍及经典用法展示

    2024-04-08 16:48:01       44 阅读
  5. linux知识点

    2024-04-08 16:48:01       26 阅读
  6. 个人网站开发记录(五)二系统后端nodejs

    2024-04-08 16:48:01       32 阅读
  7. Leetcode 1.两数之和

    2024-04-08 16:48:01       38 阅读