为了账号安全,请及时绑定邮箱和手机立即绑定

不能在函数参数中使用具有相同基础类型的类型

不能在函数参数中使用具有相同基础类型的类型

Go
郎朗坤 2022-06-06 15:06:31
我是 golang 新手,不确定问题标题是否准确,但我会尝试为问题添加更好的描述。在daos/model.go我有一个包含以下代码的包:type ActionType intconst (    ActionTypeExecute ActionType = 0 + iota    ActionTypeSelect    ActionTypeInput    ActionTypeClick    ActionTypeWait)func ActionTypeFromString(s *string) (ActionType, error) {    switch *s {    case "execute":        return ActionTypeExecute, nil    case "select":        return ActionTypeSelect, nil    case "input":        return ActionTypeInput, nil    case "click":        return ActionTypeClick, nil    case "wait":        return ActionTypeWait, nil    }    return 0, ErrInvalidActionTypeText}的目的ActionTypeFromString是将作为参数传递的字符串转换为 int。在actions/model.go我有另一个包:type ActionType stringconst (    ActionTypeExecute ActionType = "execute"    ActionTypeSelect ActionType = "select"    ActionTypeInput ActionType = "input"    ActionTypeClick ActionType = "click"    ActionTypeWait ActionType = "wait")type NewAction struct {    ActionType *ActionType `json:"type"`}func insertActionsFromScan(newActions []*NewAction) {    for _, newAction := range newActions {        actionTypeInt, err := models.ActionTypeFromString(newAction.ActionType)        if err != nil {            return nil, err        }    }}编译代码会出现以下错误,我不明白为什么,因为newAction.ActionType它是一个字符串不能在 models.ActionTypeFromString 的参数中使用 newAction.ActionType(类型 *ActionType)作为类型 *string
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

类型*string*actions.ActionType不一样,string也不actions.ActionType一样。但是,它们共享相同的基础类型,这意味着您可以将一种转换为另一种。例如(*string)(newAction.ActionType)

正如@Imaxd 在评论中提到的那样,您可以查看此答案以获取更多详细信息。


查看完整回答
反对 回复 2022-06-06
  • 1 回答
  • 0 关注
  • 189 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号