index
cate golang官网 | golang-cn | goland-github |
RSS-ATOM github | googler with python
Go 语言教程
go-gin-chat(Gin+websocket 的多人聊天室)
golang的public和private type SomeClass struct { PublicVariable string // 公共变量 privateVariable string // 私有变量 } // 公共方法 func (c *SomeClass) PublicMethod() (result string) { return "This can be called by external modules" } // 私有方法 func (c *SomeClass) privateMethod() (result string) { return "This can only be called in SomeClass" } 错误处理 func isValid(text string) (valid bool, err error) { if text == "" { return false, error("text cannot be empty") } return text == "valid text", nil }
文件读写
创建文件夹 os.Mkdir(dname, os.ModeDir|os.ModePerm) 创建文件夹及全路径 os.MkdirAll(fullpath, os.ModeDir|os.ModePerm) os.Create(fname) 删除文件 os.Remove(fname) os.Rename(oldfname, newfname) data, err := os.OpenFile("/dev/data.conf", os.O_RDWR, 0) var r io.Reader var w io.Writer r = data w = r.(io.Writer) 一次性读取文件 import "io/ioutil" if bytes, err := ioutil.ReadFile("/dev/data.conf"); err { fmt.Println(string(bytes)) } 逐行读取文件 import ( "bufio" "os" "fmt" } f, err := os.Open("/dev/data/conf") fread := bufio.NewReader(f) for { line, _, err := fread.ReadLine() if err == io.EOF { break } fmt.Println(string(line)) } 读取网页,编码转换 go get github.com/djimenez/iconv-go import "net/html" res, err := http.Get(baseUrl) if err != nil { fmt.Println(err.Error()) } else { defer res.Body.Close() utfBody, err := iconv.NewReader(res.Body, "gb2312", "utf-8") dom, err := goquery.NewDocumentFromReader(utfBody)
安装 wget https://golang.google.cn/dl/go1.18.linux-amd64.tar.gz tar -zvxf go1.18.linux-amd64.tar.gz -C /usr/local vim /etc/profile export PATH=$PATH:/usr/local/go/bin #golang env config export GO111MODULE=on export GOROOT=/usr/local/go export GOPATH=/home/gopath export PATH=$PATH:$GOROOT/bin:$GOPATH/bin #设置代理 go env -w GOPROXY=https://goproxy.cn,direct go version go env
vim-go安装
:help vim-go 安装gopls :GoInstallBinaries 配置 let g:go_fmt_command = "goimports" # Run goimports along gofmt on each save let g:go_auto_type_info = 1 # Automatically get signature/type info for object under cursor autocmd FileType html,css,xml,php,vue,wxss,wxml,go EmmetInstall autocmd FileType go set omnifunc=gocomplete#Complete 命令 :GoRun :GoBuild :GoInstall :GoDef # goto definition of object under cursor gd # also has the same effect Ctrl-O / Ctrl-I # hop back to your source file/return to definition :GoDoc # opens up a side window for quick documentationn K # also has the same effect :GoTest # run every *_test.go file and report results :GoTestFunc # or just test the function under your cursor :GoCoverage # check your test coverage :GoAlternate # switch bewteen your test case and implementation :GoImport # manage and name your imports :GoImportAs :GoDrop :GoRename # precise renaming of identifiers :GoLint # lint your code :GoVer :GoErrCheck :GoAddTags # manage your tags :GoRemoveTags
reflect & interface
Golang的gRPC也是通过反射实现的。 在Golang的实现中,每个interface变量都有一个对应pair,pair中记录了实际变量的值和类型: (value, type) 反射主要与Golang的interface类型相关(它的type是concrete type),只有interface类型才有反射一说。 reflect的基本功能TypeOf和ValueOf