go的常见操作
go的开发环境搭建:https://www.cnblogs.com/wqzn/p/11730052.html
GOROOT和GOPATH
GOROOT
和GOPATH
都是环境变量,
- 其中
GOROOT
是我们安装go开发包的路径, - GOPATH是存放go的代码目录,
从Go 1.8版本开始,Go开发包在安装完成后会为GOPATH
设置一个默认目录,参见下表。
GOPATH在不同操作系统平台上的默认值
平台 | GOPATH默认值 | 举例 |
---|---|---|
Windows | %USERPROFILE%/go | C:\Users\用户名\go |
Unix | $HOME/go | /home/用户名/go |
我们只需要记住默认的GOPATH路径在哪里就可以了。
我们需要将 GOROOT
下的bin目录及GOPATH
下的bin目录都添加到环境变量中。
配置环境变量之后需要重启你电脑上已经打开的终端。(例如cmd、VS Code里面的终端和其他编辑器的终端)。
VSCode:
安装Go语言开发工具包
在座Go语言开发的时候为我们提供诸如代码提示、代码自动补全等功能。
Windows平台按下Ctrl+Shift+P
,Mac平台按Command+Shift+P
,这个时候VS Code界面会弹出一个输入框,如下图:
设置好FQ代理:
go.toolsGopath setting is not set. Using GOPATH D:\gospace Installing 17 tools at D:\gospace\bin in module mode. gocode gopkgs go-outline go-symbols guru gorename gotests gomodifytags impl fillstruct goplay godoctor dlv gocode-gomod godef goreturns golint Installing github.com/fatih/gomodifytags SUCCEEDED Installing golang.org/x/tools/cmd/gorename SUCCEEDED Installing github.com/josharian/impl SUCCEEDED Installing golang.org/x/tools/cmd/guru SUCCEEDED Installing github.com/mdempsky/gocode SUCCEEDED Installing golang.org/x/tools/cmd/gorename SUCCEEDED Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs SUCCEEDED Installing github.com/cweill/gotests/... SUCCEEDED Installing github.com/fatih/gomodifytags SUCCEEDED Installing github.com/cweill/gotests/... SUCCEEDED Installing github.com/fatih/gomodifytags SUCCEEDED Installing github.com/ramya-rao-a/go-outline SUCCEEDED Installing github.com/josharian/impl SUCCEEDED Installing github.com/josharian/impl SUCCEEDED Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED Installing github.com/acroca/go-symbols SUCCEEDED Installing golang.org/x/tools/cmd/guru SUCCEEDED Installing golang.org/x/tools/cmd/gorename SUCCEEDED Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED Installing github.com/cweill/gotests/... SUCCEEDED Installing github.com/fatih/gomodifytags SUCCEEDED Installing github.com/josharian/impl SUCCEEDED Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED Installing github.com/godoctor/godoctor SUCCEEDED Installing github.com/godoctor/godoctor SUCCEEDED Installing github.com/godoctor/godoctor SUCCEEDED Installing github.com/godoctor/godoctor SUCCEEDED
一、file
1、go 判断文件/目录是否存在、区分文件和目录
// IsExist checks whether a file or directory exists. // It returns false when the file or directory does not exist. func IsExist(f string) bool { _, err := os.Stat(f) return err == nil || os.IsExist(err) } // IsFile checks whether the path is a file, // it returns false when it's a directory or does not exist. func IsFile(f string) bool { fi, e := os.Stat(f) if e != nil { return false } return !fi.IsDir() }
二、日志:
0.打印日志
log.Println("---------------load config-------------------")
- 直接退出
- 引发异常,并退出
这个panic
可以类比 python 中的 raise、java 中的 throw, 这样抛出的异常是一个堆栈信息,能够追溯代码错误的位置,这个在使用时我用着更顺手
恢复 panic 引发的异常使用 recover
func main(){ var s sync.WaitGroup for i:=0;i<10;i++{ s.Add(1) go func(i int){ defer func(){ if err := recover();err!=nil{ log.Println("this is error",err) } s.Done() log.Println(i,"is over") }() log.Panic(i,"i want panic") }(i) } s.Wait() }
但是需要注意,recover 之后并不会打印 panic 的堆栈信息。
三、序列化
示例
{ "time_out": 20, "user_groups": [ { "server": "192.168.1.2", "name": "myown", "port": 1234, "password": "1234567", "cipher": "AEAD_CHACHA20_POLY1305", "time_out": 10 } ] }
代码
package main import( "io/ioutil" "encoding/json" "fmt" "time" ) type UserGroup struct{ Name string `json:"name"` Server string `json:"server"` Port int `json:"port"` Password string `json:"password"` Cipher string `json:"cipher"` Key string `json:"key"` Keygen int `json:"key_gen"` UDPTimeout time.Duration `json:"time_out"` } type Config struct { UserGroups []*UserGroup `json:"user_groups"` UDPTimeout time.Duration `json:"time_out"` } /** * @msg: 读取json 文件数据,按照参数 v 的格式解析 * @param: fileName * @return: */ func LoadJsonData(fileName string, v interface{}) error{ data, err := ioutil.ReadFile(fileName) if err != nil{ return err; } dataJson := []byte(data) if err = json.Unmarshal(dataJson, v); err != nil{ return err } return nil } func main(){ /** * 异常捕获 */ defer func() { if err := recover(); err != nil{ fmt.Println("发生异常:",err) } }() var MConfig = &Config{} if err := LoadJsonData("config.json", MConfig); err != nil{ fmt.Println("读取配置错误") panic(err) } fmt.Println("获取到的数据为:", *MConfig.UserGroups[0]) }
还有比较好的例子https://blog.csdn.net/x356982611/article/details/80493296