下载编译工具时package /go-xorm/cmd/xorm: cannot download, $GOPATH not set. For more details see: go help gopath
用下面命令可解决
sudo env GOPATH=/Volumes/work/go/fox:/Volumes/work/go/fox/bin go get /go-xorm/cmd/xorm
method expression (needs pointer receiver: (*).Auth)
指针只能使用指针方式调用
service.AdminUser.Auth
要改为以下方式调用
(*service.AdminUser).Auth
method expression (needs pointer receiver: (*).Auth)
改为如下调用
var admUser *service.AdminUser
admUser.Auth(xxx)
use id (type int64) as type int in argument to
原因:
int/uint 其值范围与平台有关,所以 int32 != int int64 != int
这里要使用 自动转换
解决方法
int_id, _ := strconv.Atoi(uid)
这个时候在传入int_id
models.GetAdminById(int_id)
Struct 转 json 键名首字母小写
来源 /question/34447868
//感谢大神们回答,为方便大家参考,我这里给个例子吧
package main
import (
"encoding/json"
"fmt"
)
type ColorGroup struct {
ID int `json:"id"`
Name string `json:"name"`
Colors []string `json:"colors"`
}
func main() {
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := (group)
if err != nil {
("error:", err)
}
(string(b))
}
crashed with error runtime error: invalid memory address or nil pointer dereference
一般报这个错误原因
1.值为nil
解决方法
找到这个位置,值为nil 判断一下再输出或者赋值
use str (type interface {}) as type string in argument to : need type assertion
cannot use str (type interface {}) as type string in argument to : need type assertion
报错行
date, err = time.Parse(layout, string) 这一行报错
date = str 这一行报错
原函数
func Format(str interface{}, layout string) string {
var date
var err error
//判断变量类型
switch str.(type) {
case string:
//如果是字符串则转换成 标准日期时间格式
(str)
date, err = (layout, string)
if err != nil {
return ""
}
case :
date = str
}
return date.Format(layout)
}
解决后函数
func Format(str interface{}, layout string) string {
var date time.Time
var err error
//判断变量类型
switch str.(type) {
case string:
//如果是字符串则转换成 标准日期时间格式
(str)
date, err = time.Parse(layout, str.(string))
if err != nil {
return ""
}
case time.Time:
date = str.(time.Time)
}
return date.Format(layout)
}
use (“2006-01-02 15:04:05”) (type string) as type in assignment
或者
cannot use XXX (type string) as type XXXX in assignment
原因:
被你赋值的那个变量或结构体或者其他什么的,被你赋值的类型与要赋值的类型不符
解决:
类型转换,类型要一样才可以赋值
use arr (type []string) as type []interface {} in argument to WhereAnd
原因:[]string不能直接转换为[]interface {}类型
解决:
arr:=[]string{"A","B"}
inter := make([]interface{}, arrCount)
for y, x := range arr {
inter[y] = x
}
func WhereAnd(value ...interface{}){
.....
}