GO 语言错误处理

下载编译工具时package github.com/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 github.com/go-xorm/cmd/xorm

1.invalid method expression service.AdminUser.Auth (needs pointer receiver: (*service.AdminUser).Auth)

指针只能使用指针方式调用

service.AdminUser.Auth

要改为以下方式调用

(*service.AdminUser).Auth

2.invalid method expression service.AdminUser.Auth (needs pointer receiver: (*service.AdminUser).Auth)

改为如下调用

var admUser *service.AdminUser
admUser.Auth(xxx)

3.cannot use id (type int64) as type int in argument to models.GetAdminById

原因:

int/uint 其值范围与平台有关,所以 int32 != int int64 != int
这里要使用 strconv.Atoi 自动转换

解决方法

int_id, _ := strconv.Atoi(uid)
这个时候在传入int_id
models.GetAdminById(int_id)

4.Golang Struct 转 json 键名首字母小写

来源 https://www.zhihu.com/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 := json.Marshal(group)
    if err != nil {
        fmt.Println("error:", err)
    }
    fmt.Println(string(b))
}

5.Handler crashed with error runtime error: invalid memory address or nil pointer dereference
一般报这个错误原因
1.值为nil
解决方法
找到这个位置,值为nil 判断一下再输出或者赋值

5.cannot use str (type interface {}) as type string in argument to time.Parse: need type assertion

cannot use str (type interface {}) as type string in argument to time.Parse: need type assertion
报错行

date, err = time.Parse(layout, string) 这一行报错
date = str 这一行报错

原函数

func Format(str interface{}, layout string) string {
    var date time.Time
    var err error
    //判断变量类型
    switch str.(type) {
    case string:
        //如果是字符串则转换成 标准日期时间格式
        fmt.Println(str)
        date, err = time.Parse(layout, string)
        if err != nil {
            return ""
        }
    case time.Time:
        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:
        //如果是字符串则转换成 标准日期时间格式
        fmt.Println(str)
        date, err = time.Parse(layout, str.(string))
        if err != nil {
            return ""
        }
    case time.Time:
        date = str.(time.Time)
    }

    return date.Format(layout)
}

6.cannot use tmp.Format(“2006-01-02 15:04:05”) (type string) as type time.Time in assignment

或者
cannot use XXX (type string) as type XXXX in assignment
原因:
被你赋值的那个变量或结构体或者其他什么的,被你赋值的类型与要赋值的类型不符
解决:
类型转换,类型要一样才可以赋值

7.cannot 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{}){
    .....
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风.foxwho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值