汇总 Golang 经常使用的第三方库,比如 cobra 等
介绍
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
// Open File
f, err := os.Open("/usr/local/Cellar/graphviz/10.0.1/share/doc/graphviz/libguide.pdf")
if err != nil {
panic(err)
}
defer f.Close()
buffer := make([]byte, 512)
if _, err := f.Read(buffer); err != nil {
panic(err)
}
contentType := http.DetectContentType(buffer)
fmt.Println("Content Type: " + contentType)
}
- sturct
- common-nighthawk/go-figure 输出 ASCII 码艺术字
- gorilla
- Masterminds/semver 提供了在 Go 中使用语义版本的能力
- Semantic Versioning
- 主版本号:当你做了不兼容的 API 修改
- 次版本号:当你做了向下兼容的功能性新增
- 修订号:当你做了向下兼容的问题修正
- 正则
^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?
(参考)
v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?
- date
- shirou/gopsutil Python 工具库 psutil 的 Golang 移植版
- html/template
- PuerkitoBio/goquery A little like that j-thing, only in Go.
- goreman 是 go 语言编写的多进程管理工具,对 ruby 的 foreman 的重写
- bits-and-blooms/bitset 位运算
- RoaringBitmap/roaring Go (golang) 中的处理位图库,由 InfluxDB、Bleve 和 DataDog 使用
- gore 另一款运行良好的 Go REPL。具有行编辑、代码自动补全等功能。CMD 单步执行 golang 命令
- hajimehoshi/ebiten 适用于 Go 的简单 2D 游戏引擎
demo
req
package main
import (
"fmt"
"github.com/imroc/req"
)
func main() {
req.Debug = true
headers := req.Header{
"content-type": "application/json",
}
resp, err := req.Get("https://api.github.com", headers)
fmt.Println(resp, err, resp.Dump())
}
websocket
package main
import (
"io"
"net/http"
"golang.org/x/net/websocket"
)
func echoServer(ws *websocket.Conn) {
defer ws.Close()
io.Copy(ws, ws)
}
func main() {
http.Handle("/", websocket.Handler(echoServer))
err := http.ListenAndServe(":12345", nil)
panic(err)
}
package main
import (
"fmt"
"golang.org/x/net/websocket"
)
func main() {
origin := "http://localhost/"
url := "ws://localhost:12345/ws"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
panic(err)
}
if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
panic(err)
}
var msg = make([]byte, 512)
var n int
if n, err = ws.Read(msg); err != nil {
panic(err)
}
fmt.Printf("Received: %s.\n", msg[:n])
// or
msgToSend := "Hello"
err = websocket.Message.Send(ws, msgToSend)
var msgToReceive string
err = websocket.Message.Receive(ws, &msgToReceive)
fmt.Println(msgToSend)
}