为什么我无法使用在不同包中定义的结构?

use*_*878 0 go

我无法使用package main已在其他包中定义的结构.请注意我正确导入其他包

我将结构及其字段命名为以大写字母开头,因为我在Golang中读到了这就是我们如何表明它是一个导出的字段.虽然导入包不需要.

fsm.go

package fsm

import (
"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
}
Run Code Online (Sandbox Code Playgroud)

test.go

package main

import (
"encoding/json"
"fmt"

"github.com/looplab/fsm"
) 
func main(){
    Transitions := make(map[EKey]string) 
}
Run Code Online (Sandbox Code Playgroud)

Error: undefined EKey

icz*_*cza 8

您必须先导入要引用其标识符的包:

import "path/to/fsm"
Run Code Online (Sandbox Code Playgroud)

执行此操作后,程序包名称fsm将成为文件块中的新标识符,您可以使用限定标识符来引用其导出的标识符(以大写字母开头的标识符),packagename.IdentifierName如下所示:

Transitions := make(map[fsm.EKey]string)
Run Code Online (Sandbox Code Playgroud)

请参阅相关问题:在没有选择器错误的情况下使用包