为什么我无法使用在其他程序包中定义的结构?

时间:2019-01-15 10:26:25

标签: 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
}

test.go

package main

import (
"encoding/json"
"fmt"

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

Error: undefined EKey

3 个答案:

答案 0 :(得分:8)

您必须首先import要引用其identifiers的软件包:

import "path/to/fsm"

执行此操作后,程序包名称fsm将成为file block中的新标识符,您可以使用以下命令引用其exported identifiers(以大写字母开头的标识符) qualified identifier,即packagename.IdentifierName,如下所示:

Transitions := make(map[fsm.EKey]string)

查看相关问题:Getting a use of package without selector error

答案 1 :(得分:2)

您需要使用IIfcSurfaceStyle来引用您的结构

如果要将其导入到本地名称空间,则在导入路径前需要一个点。

fsm.EKey

现在您可以将结构直接称为import ( // .... . "github.com/looplab/fsm" )

答案 2 :(得分:1)

尝试

package main

import (
"encoding/json"
"fmt"

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