GoLang错误集合
# 前言
使用GoLang
中遇到的奇葩问题,本文持续更新
# conversion from int to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)
翻译:
从int
到字符串的转换产生一个符文字符串,而不是一个数字字符串(你是指fmt.Sprint(x)
吗?)
解决方案:
// 产生错误的写法,会导致编译不通过
func Day() string {
return string(time.Now().Day())
}
// 正确的写法
func Day() string {
return fmt.Sprintf("%v", time.Now().Day())
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# missing dot in first path element
问题解决
我出现这个原因是使用开源代码的时候将原始的引用改变了但是忘记改变go.mod
中的嵌套引用了。
# 问题原因
因为在go1.13
中,go module
名称规范要求路径的第一部分必须满足域名规范,否则可能汇报类似
malformed module path "xxxx": missing dot in first path element
这样的错误。
# panic: Get "https://xingcxb.com/": proxyconnect tcp: EOF
# 问题原因
这个问题大概率是在执行 http
请求的使用代理时代理设置异常,基本上就是在构建代理字符串的时候把http
写成了https
,正确和错误代码看下面
# 错误写法
proxyStr := fmt.Sprintf("https://%s:%s@%s", username, password, dynamicIp)
1
# 正确写法
proxyStr := fmt.Sprintf("http://%s:%s@%s", username, password, dynamicIp)
1
# sql: expected 8 destination arguments in Scan, not 1
# 错误原因
数据库不只1个字段,而你只获取1个字段,所以报错
# 解决方案
限制sql
查询出来的内容或者Scan
中的变量适应数据库中的字段
// 错误写法
var result map[string]interface{}
err := common.DbInfo.QueryRow("select * from `ua` where id = ?", randomInt).Scan(&result)
// 限制sql写法
var result string
err := common.DbInfo.QueryRow("select user_agent from `ua` where id = ?", randomInt).Scan(&result)
// 适应数据库的写法
type resultObj struct {
Id int `id`
UserAgent string `user_agent`
}
var result *resultObj
err := common.DbInfo.QueryRow("select * from `ua` where id = ?", randomInt).Scan(&result)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
更新时间: 2023/9/16 16:06:58