补充资料.
Signed-off-by: chen.yang <chen.yang@yuzhen-iot.com>
This commit is contained in:
parent
786449d2ed
commit
075ca1c6e9
|
@ -115,6 +115,10 @@
|
||||||
* [Qt 实战一二三](https://blog.csdn.net/liang19890820/article/details/50277095)
|
* [Qt 实战一二三](https://blog.csdn.net/liang19890820/article/details/50277095)
|
||||||
* [Qt 学习之路 2](https://www.devbean.net/2012/08/qt-study-road-2-catelog/)
|
* [Qt 学习之路 2](https://www.devbean.net/2012/08/qt-study-road-2-catelog/)
|
||||||
|
|
||||||
|
## Golang 资料
|
||||||
|
|
||||||
|
* [Awesome Go](https://awesome-go.com/)
|
||||||
|
|
||||||
## 软件资源
|
## 软件资源
|
||||||
|
|
||||||
* [Download Qt](http://download.qt.io/)
|
* [Download Qt](http://download.qt.io/)
|
||||||
|
|
|
@ -0,0 +1,101 @@
|
||||||
|
# Golang 中的 Error 类型
|
||||||
|
|
||||||
|
error 类型本身就是一个预定义好的接口,里面定义了一个 method
|
||||||
|
|
||||||
|
```go
|
||||||
|
type error interface {
|
||||||
|
Error() string
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
生成一个新的 error 并返回
|
||||||
|
|
||||||
|
一般有以下几种处理方式:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Customerror struct {
|
||||||
|
infoa string
|
||||||
|
infob string
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cerr Customerror) Error() string {
|
||||||
|
errorinfo := fmt.Sprintf("infoa : %s , infob : %s , original err info : %s ", cerr.infoa, cerr.infob, cerr.Err.Error())
|
||||||
|
return errorinfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// 方法一:
|
||||||
|
// 采用 errors 包的 New 方法 返回一个 err 的类型
|
||||||
|
var err error = errors.New("this is a new error")
|
||||||
|
// 由于已经实现了 error 接口的方法 因此可以直接调用对应的方法
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
|
||||||
|
// 方法二:
|
||||||
|
// 采用 fmt.Errof 将 string 信息转化为 error 信息 并返回
|
||||||
|
err = fmt.Errorf("%s", "the error test for fmt.Errorf")
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
|
||||||
|
// 方法三:
|
||||||
|
// 采用自定义的方式实现一个 error 的 一个 duck 类型
|
||||||
|
err = &Customerror{
|
||||||
|
infoa: "err info a",
|
||||||
|
infob: "err info b",
|
||||||
|
Err: errors.New("test custom err"),
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*output:
|
||||||
|
this is a new error
|
||||||
|
the error test for fmt.Errorf
|
||||||
|
infoa : err info a , infob : err info b , original err info : test custom err
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
golang 中的 error package 内容也比较简单,这个 package 中实现了 error 中所声明的 method(Error)相当于是一个 error 接口的 duck 类型。
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Package errors implements functions to manipulate errors.
|
||||||
|
package errors
|
||||||
|
|
||||||
|
// New returns an error that formats as the given text.
|
||||||
|
func New(text string) error {
|
||||||
|
return &errorString{text}
|
||||||
|
}
|
||||||
|
|
||||||
|
// errorString is a trivial implementation of error.
|
||||||
|
type errorString struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errorString) Error() string {
|
||||||
|
return e.s
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
采用 fmt.Errorf 方法把 string 类型转化为 error 类型,在这个方法的内部,先调用 fmt 包中的 Sprintf 方法把格式化的输入转化为字符串,在使用 errors.New 方法返回 error 类型。
|
||||||
|
|
||||||
|
采用自定义的 error 类型可以先判断 err 的动态类型,再进行下一层的判断。
|
||||||
|
比如 net.Error 接口,是一个对原先 error 接口的再封装。在读取文件的时候判断读取器读取结束的时候的 io.EOF。
|
||||||
|
|
||||||
|
```go
|
||||||
|
//io.EOF
|
||||||
|
var EOF = errors.New("EOF")
|
||||||
|
|
||||||
|
//net.Error
|
||||||
|
type Error interface {
|
||||||
|
error
|
||||||
|
Timeout() bool // Is the error a timeout?
|
||||||
|
Temporary() bool // Is the error temporary?
|
||||||
|
}
|
||||||
|
```
|
|
@ -0,0 +1,31 @@
|
||||||
|
# Go 使用私有仓库
|
||||||
|
|
||||||
|
1. 在服务器上创建 go package 仓库并提交代码,注意 go.mod 的 path 需要与服务器路径一致,例如:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 服务器地址: gitea.com/project/package.git
|
||||||
|
// go.mod 文件内容如下
|
||||||
|
module gitea.com/project/package
|
||||||
|
go 1.16
|
||||||
|
```
|
||||||
|
|
||||||
|
2. 设置 go 的环境变量
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go env -w GOPRIVATE=你的域名1;你的域名2
|
||||||
|
```
|
||||||
|
|
||||||
|
3. 使用 git 的 netrc 保存登录信息
|
||||||
|
|
||||||
|
```bash
|
||||||
|
touch ~/.netrc
|
||||||
|
sudo chmod +755 ~/.netrc
|
||||||
|
vi ~/.netrc
|
||||||
|
echo "machine 你的域名 login 你的用户名 password 你的TOKEN或口令" > ~/.netrc
|
||||||
|
# 例如
|
||||||
|
echo "machine github.com login USERNAME password APIKEY" > ~/.netrc
|
||||||
|
```
|
||||||
|
|
||||||
|
## 外部参考
|
||||||
|
|
||||||
|
1.[Go 私有仓库模块拉取](https://blog.csdn.net/q1009020096/article/details/108421435)
|
|
@ -1,4 +1,4 @@
|
||||||
# [有关 Package 中 init 方法的多处定义及运行顺序问题](https://blog.csdn.net/zhuxinquan61/article/details/73712251)
|
# [Go 有关 Package 中 init 方法的多处定义及运行顺序问题](https://blog.csdn.net/zhuxinquan61/article/details/73712251)
|
||||||
|
|
||||||
在不了解这个问题之前,在网上搜索一下竟然搜出了两个完全相反的结果,所以打算自己测试下这个问题。
|
在不了解这个问题之前,在网上搜索一下竟然搜出了两个完全相反的结果,所以打算自己测试下这个问题。
|
||||||
|
|
Loading…
Reference in New Issue