Go 无法在函数参数中使用具有相同基础类型的类型
我是刚来golang的新手,不确定问题标题是否准确,但我会尝试为问题添加更好的描述 在daos/model.go中,我有一个包含以下代码的包:Go 无法在函数参数中使用具有相同基础类型的类型,go,Go,我是刚来golang的新手,不确定问题标题是否准确,但我会尝试为问题添加更好的描述 在daos/model.go中,我有一个包含以下代码的包: type ActionType int const ( ActionTypeExecute ActionType = 0 + iota ActionTypeSelect ActionTypeInput ActionTypeClick ActionTypeWait ) func ActionTypeFromStrin
type ActionType int
const (
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的目的是将作为param传递的字符串转换为int
在actions/model.go中,我有另一个包:
type ActionType string
const (
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作为type*string
类型*string和*actions.ActionType不相同,string和actions.ActionType也不相同。但是,它们共享相同的基础类型,这意味着您可以将一个转换为另一个。e、 g.*stringnewAction.ActionType
正如@Imaxd在评论中提到的,您可以查看更多详细信息。错误是不言自明的:type ActionType string引入了一个新类型。你必须投给*string@mkopriva这很管用,我在发帖前尝试了一些转换,但不是这样。谢谢你,也许可以加上你的评论作为回答。为了更深入的理解,你可以提到这个相关的问题