介绍 只是列举了,常用的代码片段,封装成函数,直接调用,如果想修改其他功能直接替换调核心代码即可,方便下次直接使用。
ps:写Java写习惯了,喜欢用一个类表示一个功能
代码放在了GitHub上面:https://github.com/fz1lin/goFragment
参考链接 https://blog.gm7.org/个人知识库/03.编程开发/GO/02.GO常用实例.html
https://go-zh.org/pkg/
https://learnku.com/docs/build-web-application-with-golang/about-this-book/3151
http://c.biancheng.net/view/2.html
I/O读写文件 代码路径 /readWrite
各种格式的速度以及大小对比
后缀
读取
写入
大小(KiB)
读/写LOC
格式
.gob
0.3
0.2
7948
21 + 11 =32
Go二进制
.gob.gz
0.5
1.5
2589
21 + 11 =32
Go二进制
.jsn
4.5
2.2
16283
32+17 = 49
JSON
.jsn.gz
4.5
3.4
2678
21 + 11 =32
JSON
.xml
6.7
1.2
18917
45 + 30 = 75
XML
.xml.gz
6.9
2.7
2730
45 + 30 = 75
XML
.txt
1.9
1.0
12375
86 + 53 = 139
纯文本(UTF-8)
.txt.gz
2.2
2.2
2514
86 + 53 = 139
纯文本(UTF-8)
.inv
1.7
3.5
7250
128 + 87 = 215
自定义二进制
.inv.gz
1.6
2.6
2400
128 + 87 = 215
自定义二进制
读取文件 按照字节读取内容,返回读取的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 func ReadFileFunc (fileName string ) string { file, err := os.Open(fileName) if err != nil { return "文件打开失败,没有" + fileName + "文件" } defer file.Close() buf := make ([]byte , 1024 ) var res string for { count, err := file.Read(buf) if err == io.EOF { break } else { currBytes := buf[:count] res += string (currBytes) } } return res }
写入文件 写入到一个新的文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 func WriteFileFunc (writeString string , newfileName string ) bool { file, err := os.Create(newfileName) if err != nil { fmt.Println(err) } defer file.Close() _, err = io.WriteString(file, writeString) if err != nil { fmt.Println(err) } return true }
正则表达式 常用正则
https://github.com/cdoco/learn-regex-zh
https://github.com/jaywcjlove/regexp-example
也可以借助chatGPT,你发送一个字符片段,你给他说,我要使用go语言,正则提取xxx内容,就会返回一个正则表达式。
提取 go内置语法参考
https://go-zh.org/pkg/regexp/
提取,根据正则表达式,提取对应的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 func RegexFunc (regexString string , value string ) string { var res string regex, _ := regexp.Compile(regexString) matches := regex.FindAllString(value, -1 ) for _, res = range matches { } return res }
JSON 相关处理 JSON转换为map 1 2 3 4 5 6 7 8 9 10 11 12 13 func JsonSwitchMap (jsonStr string ) map [string ]interface {} { var data map [string ]interface {} err := json.Unmarshal([]byte (jsonStr), &data) if err != nil { fmt.Println(err) } return data }
JSON转换为切片 1 2 3 4 5 6 7 8 9 10 11 func JsonSwitchSlice (jsonStr string ) []map [string ]interface {} { var data []map [string ]interface {} err := json.Unmarshal([]byte (jsonStr), &data) if err != nil { fmt.Println(err) } return data }
map转换为JSON 传入json数据
1 2 3 4 5 mapData := map [string ]interface {}{ "name" : "John" , "age" : 30 , "email" : "example.com" , }
转换
1 2 3 4 5 6 7 8 9 10 11 12 13 func MapSwitchJson (mapData map [string ]interface {}) string { jsonBytes, err := json.Marshal(mapData ) if err != nil { fmt.Println(err) } jsonString := string (jsonBytes) return jsonString }
JSON序列化 序列化指将 Go 语言数据结构转换为 JSON 格式的字符串。可以使用 json.Marshal()
函数来实现序列化操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 type Address struct { Street string `json:"street"` City string `json:"city"` State string `json:"state"` } type Person struct { Name string `json:"name"` Age int `json:"age"` Address Address `json:"address"` } func main () { person := Person{ Name: "Alice" , Age: 25 , Address: Address{ Street: "123 Main St" , City: "Anytown" , State: "CA" , }, } jsonString, err := json.Marshal(person) if err != nil { log.Fatal(err) } fmt.Println(string (jsonString)) }
使用 json.Marshal()
函数序列化时,结构体成员名称的首字母必须大写,否则该成员将不会被序列化。为了指定 JSON 字段的名称,可以使用结构体标记(tag)来标注成员
JSON反序列化 反序列化指将 JSON 格式的字符串转换为对应的 Go 语言数据结构。可以使用 json.Unmarshal()
函数来实现反序列化操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 type Addresss struct { Street string `json:"street"` City string `json:"city"` State string `json:"state"` } type Persons struct { Name string `json:"name"` Age int `json:"age"` Address Address `json:"address"` } func main () { jsonString := `{"name": "Alice", "age": 25, "address": {"street": "123 Main St", "city": "Anytown", "state": "CA"}}` var persons Persons err := json.Unmarshal([]byte (jsonString), &persons) if err != nil { log.Fatal(err) } fmt.Printf("%+v\n" , persons) }
json包 导入json包
1 2 go get github.com/json-iterator/go
时间日期 /times包下
1 2 3 4 5 6 7 8 9 10 11 12 func main () { now := time.Now() fmt.Println(now) fmt.Println(now.Unix()) fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan" )) fmt.Println(now.Format("2006-01-02 15:04:05" )) fmt.Println(now.Format("20060102" )) }
函数运行时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 func TimeSince (f func () ) string { start := time.Now() f() elapsed := time.Since(start) str := fmt.Sprintf("该函数执行完成耗时:%v" , elapsed) return str } func main () { result := TimeSince(timeTest) fmt.Println(result) } func timeTest () { sum := 0 for i := 0 ; i < 1111111111 ; i++ { sum++ } }
go并发读取txt
随机数 生成随机数 1 2 3 4 5 6 7 8 9 func RandomSum (start int , end int ) int { rand.Seed(time.Now().Unix()) randomInt := rand.Intn(end-start+1 ) + start return randomInt }
生成随机字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 const ( letterBytes = "abcdefghijklmnopqrstuvwxyz" capitalBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" numberBytes = "0123456789" symbolBytes = "!@#$%^&*()" ) func RandStringBytes (n int , randomString string ) string { rand.Seed(time.Now().Unix()) b := make ([]byte , n) for i := range b { b[i] = randomString[rand.Intn(len (randomString))] } return string (b) } func main () { randomString := RandStringBytes(10 , letterBytes+numberBytes) fmt.Println(randomString) }
URL解析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 func main () { urlString := "https://www.example.com/path/to/resource?key1=value1&key2=value2#fragment" parsedUrl, err := url.Parse(urlString) if err != nil { panic (err) } fmt.Println("Scheme:" , parsedUrl.Scheme) fmt.Println("Host:" , parsedUrl.Host) fmt.Println("Path:" , parsedUrl.Path) fmt.Println("RawQuery:" , parsedUrl.RawQuery) fmt.Println("Fragment:" , parsedUrl.Fragment) query := parsedUrl.Query() fmt.Println("key1:" , query.Get("key1" )) fmt.Println("key2:" , query.Get("key2" )) }
base64 1 2 3 4 5 6 7 8 9 10 11 12 13 func base64Encode (encodeStr string ) string { return base64.StdEncoding.EncodeToString([]byte (encodeStr)) } func Base64Decode (decodeStr string ) string { decoded, err := base64.StdEncoding.DecodeString(decodeStr) if err != nil { panic (err) } return string (decoded) }
命令行参数 标准库(不推荐) parseArgs为主要的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package mainimport ( "fmt" "os" ) func main () { argsMap := parseArgs() if argA, ok := argsMap["-a" ]; ok { fmt.Println(argA) } if _, ok := argsMap["-b" ]; ok { test() } } func ParseArgs () map [string ]string { argsMap := make (map [string ]string ) for i := 1 ; i < len (os.Args); i++ { arg := os.Args[i] if i < len (os.Args)-1 { argsMap[arg] = os.Args[i+1 ] } else { argsMap[arg] = "" } } return argsMap } func test () { fmt.Println("test" ) }
Cobra 库(推荐) https://github.com/spf13/cobra
以下代码示例
安装和导入
举例子
1 2 hugo server --port=1313 git clone URL --bare
1 2 3 4 5 6 7 go get -u github.com/spf13/cobra@latestgo install github.com/spf13/cobra-cli@latestimport "github.com/spf13/cobra"
项目目录下运行初始化
会创建一个cmd/root.go
和main.go
root.go是这个地方的描述
增加command 命令
生成一个cmd/argPage.go
cmd/argPage.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package cmdimport ( "fmt" "github.com/spf13/cobra" "os" ) var argPageCmd = &cobra.Command{ Use: "argPage" , Short: "A brief description of your command" , Long: `A longer description that spans multiple lines and likely contains examples and usage of using your command. For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.` , PersistentPreRun: func (cmd *cobra.Command, args []string ) { if len (args) == 0 && len (os.Args) == 1 { cmd.HelpFunc()(cmd, args) os.Exit(0 ) } }, Run: runArgPage, } func init () { rootCmd.AddCommand(argPageCmd) argPageCmd.PersistentFlags().StringP("All" , "a" , "" , "All to use" ) } func runArgPage (cmd *cobra.Command, args []string ) { All, err := cmd.Flags().GetString("All" ) if err != nil { } if All != "" { fmt.Println("All is:" , All) } }
main.go
1 2 3 4 5 6 7 package mainimport "awesomeProject/cmd" func main () { cmd.Execute() }
增加root flag标志位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 package cmdimport ( "fmt" "os" "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ Use: "./awesomeProject" , Short: "A brief description of your application" , Long: `A longer description that spans multiple lines and likely contains to quickly create a Cobra application.` , PersistentPreRun: func (cmd *cobra.Command, args []string ) { if len (args) == 0 && len (os.Args) == 1 { cmd.HelpFunc()(cmd, args) os.Exit(0 ) } }, Run: run, } func Execute () { err := rootCmd.Execute() if err != nil { os.Exit(1 ) } } func init () { rootCmd.Flags().StringP("location" , "l" , "" , "Location to use" ) rootCmd.Flags().StringP("url" , "u" , "" , "select url" ) } func run (cmd *cobra.Command, args []string ) { location, err := cmd.Flags().GetString("location" ) if err != nil { } if location != "" { fmt.Println("Location is:" , location) } url, err := cmd.Flags().GetString("url" ) if err != nil { } if url != "" { fmt.Println("url is" +url) } }
完整代码路径
/cmd/root.go,/cmd/argPage.go ,main.go
执行系统命令 1 2 3 4 5 6 7 8 9 10 func main () { cmd := exec.Command("ls" , "-la" ) output, err := cmd.Output() if err != nil { fmt.Println(err) return } fmt.Println(string (output)) }
HTTP nc监听go发送的包
发送get请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 func main () { m := GetFunc("https://baidu.com" ) fmt.Println(m["code" ]) } func GetFunc (url string ) map [string ]interface {} { valueMap := make (map [string ]interface {}) response, err := http.Get(url) if err != nil { fmt.Println("Error while getting the response:" , err) } defer response.Body.Close() code := response.StatusCode header := response.Header.Get("Server" ) body, err := io.ReadAll(response.Body) if err != nil { fmt.Println(err) } valueMap["code" ] = code valueMap["header" ] = header valueMap["body" ] = string (body) return valueMap }
必须要传自定义的header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 package mainimport ( "fmt" jsoniter "github.com/json-iterator/go" "io" "net/http" "time" ) func main () { url := "http://httpbin.org/get" headerCustom := map [string ]string { "User-Agent" : "123" , "name" : "771" , } headerFunc := GetCustomHeaderFunc(url, headerCustom) fmt.Println(headerFunc["headers" ]) } func GetCustomHeaderFunc (url string , headerMap interface {}) map [string ]interface {} { req, err := http.NewRequest("GET" , url, nil ) respMap := make (map [string ]interface {}) if err != nil { fmt.Println(err) } m := headerMap.(map [string ]string ) var headerKey string var headerValue string for k, v := range m { headerKey = k headerValue = v } req.Header.Set(headerKey, headerValue) client := http.Client{ Timeout: 3 * time.Second, } resp, err := client.Do(req) if err != nil { fmt.Println(err) } body, _ := io.ReadAll(resp.Body) headers := jsoniter.Get(body, "headers" ) respMap["body" ] = string (body) respMap["headers" ] = headers.ToString() return respMap }
1 2 3 4 5 6 7 8 { "Accept-Encoding" : "gzip" , "Host" : "httpbin.org" , "Name" : "771" , "User-Agent" : "Go-http-client/1.1" , "X-Amzn-Trace-Id" : "Root=1-6437f80d-659f86f30de19c1d031f51cd" }
发送post请求(自定义) 简单的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 func SendPostRequest (url string , payload []byte , headers map [string ]string ) (*http.Response, error) { req, err := http.NewRequest("POST" , url, bytes.NewBuffer(payload)) if err != nil { return nil , err } for key, value := range headers { req.Header.Set(key, value) } client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil , err } return resp, nil } func main () { url := "https://example.com/api" payload := []byte (`{"key": "value"}` ) headers := map [string ]string { "Content-Type" : "application/json" , "Authorization" : "Bearer abc123" , } resp, err := SendPostRequest(url, payload, headers) if err != nil { fmt.Println(err) return } body := resp.Body fmt.Println(body) }
发送的结果
1 2 3 4 5 6 7 POST /api HTTP/1.1 Host: example.com Content-Type: application/json Content-Length: 23 Authorization: Bearer abc123 {"key" : "value" }
忽略证书 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 func main () { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true }, } client := &http.Client{Transport: tr} resp, err := client.Get("https://example.com" ) if err != nil { fmt.Println(err) return } defer resp.Body.Close() fmt.Println(resp.Status) }
代理 http代理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 func SetGlobalHttpProxy (proxyServer string ) error { if proxyServer == "" { return nil } proxyURL, err := url.Parse(proxyServer) if err != nil { return fmt.Errorf("Error parsing proxy server address: %s" , err) } http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyURL)} return nil } func main () { proxyServer := "http://127.0.0.1:123" if err := SetGlobalHttpProxy(proxyServer); err != nil { fmt.Println(err) return } }
socks代理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package proxysimport ( "fmt" "golang.org/x/net/proxy" "net/http" "net/url" "time" ) func SetGlobalSocksProxy (proxyServer string ) error { if proxyServer == "" { return nil } proxyURL, err := url.Parse(proxyServer) if err != nil { return fmt.Errorf("Error parsing proxy server address: %s" , err) } dialer, err := proxy.FromURL(proxyURL, proxy.Direct) if err != nil { return fmt.Errorf("Error creating SOCKS5 dialer: %s" , err) } http.DefaultTransport = &http.Transport{ Dial: dialer.Dial, DialContext: dialer.(proxy.ContextDialer).DialContext, DialTLS: dialer.Dial, DialTLSContext: dialer.(proxy.ContextDialer).DialContext, TLSHandshakeTimeout: 10 * time.Second, } return nil } func main () { if err := SetGlobalSocksProxy("127.0.0.1:123" ); err != nil { fmt.Println(err) return } }
并发
生产者每秒生成一个字符串,并通过通道传给消费者,生产者使用两个 goroutine 并发运行,消费者在 main() 函数的 goroutine 中进行处理。
仅仅通过关键字 go 实现 goroutine,和通道实现数据交换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package mainimport ( "fmt" "math/rand" "time" ) func producer (header string , channel chan <- string ) { for { channel <- fmt.Sprintf("%s: %v" , header, rand.Int31()) time.Sleep(time.Second) } } func customer (channel <-chan string ) { for { message := <-channel fmt.Println(message) } } func main () { channel := make (chan string ) go producer("cat" , channel) go producer("dog" , channel) customer(channel) }
1 2 3 4 5 6 7 8 9 cat: 402107940 dog: 512906503 cat: 1348830552 dog: 272199843 cat: 604152205 dog: 881161598 dog: 933967425 cat: 1342381351
并发发送http请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 package mainimport ( "fmt" "io" "net/http" "sync" "time" ) type Response struct { Url string StatusCode int Body string Error error } func fetchURL (url string , wg *sync.WaitGroup, ch chan <- Response) { defer wg.Done() resp, err := http.Get(url) if err != nil { ch <- Response{Url: url, Error: err} return } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { ch <- Response{Url: url, Error: err} return } ch <- Response{Url: url, StatusCode: resp.StatusCode, Body: string (body)} } func fetchAllURLs (urls []string , maxConcurrency int ) []Response { ch := make (chan Response, len (urls)) var wg sync.WaitGroup for _, url := range urls { wg.Add(1 ) go fetchURL(url, &wg, ch) } wg.Wait() close (ch) var results []Response for resp := range ch { results = append (results, resp) } return results } func main () { start := time.Now() urls := []string { "https://www.google.com" , "https://www.facebook.com" , "https://www.apple.com" , "https://www.amazon.com" , } responses := fetchAllURLs(urls, 1 ) for _, resp := range responses { if resp.Error != nil { fmt.Printf("Error fetching %s: %s\n" , resp.Url, resp.Error) } else { fmt.Printf("%s returned status %d, body length %d\n" , resp.Url, resp.StatusCode, len (resp.Body)) } } elapsed := time.Since(start) fmt.Println("该函数执行完成耗时:" , elapsed) }
web 服务器 类似于,python3 -m http.server
1 2 3 4 func main () { http.Handle("/" , http.FileServer(http.Dir("." ))) http.ListenAndServe(":8082" , nil ) }
windows 编译多个平台程序 设置GOOS
和GOARCH
环境变量来指定目标平台的操作系统和CPU架构
Windows 64位:GOOS=windows
,GOARCH=amd64
Windows 32位:GOOS=windows
,GOARCH=386
macOS 64位:GOOS=darwin
,GOARCH=amd64
Linux 64位:GOOS=linux
,GOARCH=amd64
Linux 32位:GOOS=linux
,GOARCH=386
Linux ARMv5(如树莓派):GOOS=linux
,GOARCH=arm
,GOARM=5
Linux ARMv6(如树莓派Zero):GOOS=linux
,GOARCH=arm
,GOARM=6
Linux ARMv7(如树莓派2):GOOS=linux
,GOARCH=arm
,GOARM=7
Linux ARM64v8(如树莓派3):GOOS=linux
,GOARCH=arm64
进入项目目录底下
Linux 64 位具体参考如上
1 2 3 SET GOOS=linux SET GOARCH=amd64 go build -o linuxTest
window 64 位具体参考如上
1 2 3 export GOOS=windows export GOARCH=amd64 go build -o winTest.exe
macOS 1 2 3 SET GOOS=darwin SET GOARCH=amd64 go build -o macTest
banner生成 http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something