课程进度 59% · 第14/23章第14/23章 · 标签 1/2
— 1 —
常用包介绍
Go标准库提供了丰富的功能包:
go
1
import (
2
"fmt" // 格式化输入输出
3
"strings" // 字符串操作
4
"time" // 时间处理
5
"math" // 数学函数
6
"net/http" // HTTP客户端/服务端
7
"encoding/json" // JSON处理
8
)
9
10
// strings使用
11
strings.Contains("hello", "ll")
12
strings.Split("a,b,c", ",")
13
strings.Join([]string{"a","b"}, "-")
14
15
// time使用
16
now := time.Now()
17
fmt.Println(now.Format("2006-01-02 15:04:05"))
— 2 —
文件与网络编程
go
1
// 文件操作
2
data := []byte("hello")
3
os.WriteFile("test.txt", data, 0644)
4
content, _ := os.ReadFile("test.txt")
5
6
// HTTP服务器
7
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
8
fmt.Fprintf(w, "Hello")
9
})
10
http.ListenAndServe(":8080", nil)
fmtstringstimeencoding/jsonnet/http