猿问

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

我是 golang 新手,不确定问题标题是否准确,但我会尝试为问题添加更好的描述。


在daos/model.go我有一个包含以下代码的包:


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是将作为参数传递的字符串转换为 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)作为类型 *string


郎朗坤
浏览 190回答 1
1回答

catspeake

类型*string和*actions.ActionType不一样,string也不actions.ActionType一样。但是,它们共享相同的基础类型,这意味着您可以将一种转换为另一种。例如(*string)(newAction.ActionType)。正如@Imaxd 在评论中提到的那样,您可以查看此答案以获取更多详细信息。
随时随地看视频慕课网APP

相关分类

Go
我要回答