在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
go的开发环境搭建:https://www.cnblogs.com/wqzn/p/11730052.html GOROOT和GOPATH
从Go 1.8版本开始,Go开发包在安装完成后会为 GOPATH在不同操作系统平台上的默认值
我们只需要记住默认的GOPATH路径在哪里就可以了。 我们需要将 配置环境变量之后需要重启你电脑上已经打开的终端。(例如cmd、VS Code里面的终端和其他编辑器的终端)。 VSCode: 安装Go语言开发工具包 在座Go语言开发的时候为我们提供诸如代码提示、代码自动补全等功能。 Windows平台按下
设置好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
一、file1、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 引发的异常使用 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
|
请发表评论