亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不能在函數參數中使用具有相同基礎類型的類型

不能在函數參數中使用具有相同基礎類型的類型

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 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號