我無法使用package main在不同包中定義的結構。請注意,我正在正確導入其他包我以大寫字母開頭命名結構及其字段,因為我在 Golang 中讀到,這就是我們指示它是導出字段的方式。盡管如果包是導入的則不需要。fsm.gopackage fsmimport ("fmt""strings" )// EKey is a struct key used for storing the transition map.type EKey struct {// event is the name of the event that the keys refers to.Event string// src is the source from where the event can transition.Src string}測試.gopackage mainimport ("encoding/json""fmt""github.com/looplab/fsm") func main(){ Transitions := make(map[EKey]string) }Error: undefined EKey
3 回答

qq_笑_17
TA貢獻1818條經驗 獲得超7個贊
您必須首先導入要引用其標識符的包:
import?"path/to/fsm"
執行此操作后,包名稱將成為文件塊fsm
中的新標識符,您可以使用限定標識符引用其導出的標識符(以大寫字母開頭的標識符),如下所示:packagename.IdentifierName
Transitions?:=?make(map[fsm.EKey]string)

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
您需要使用來引用您的結構fsm.EKey
如果要將其導入本地名稱空間,則需要在導入路徑前加一個點。
import (
// ....
. "github.com/looplab/fsm"
)
現在您可以直接將您的結構稱為EKey

不負相思意
TA貢獻1777條經驗 獲得超10個贊
嘗試這個
package main
import (
"encoding/json"
"fmt"
"github.com/looplab/fsm"
)
func main(){
Transitions := make(map[fsm.EKey]string)
}
- 3 回答
- 0 關注
- 179 瀏覽
添加回答
舉報
0/150
提交
取消