I am unable to use a struct in package main
which has been defined in a different package. Please note that I am importing the other package correctly
I named the struct and its fields starting with a capital letter because I read that in Golang that is how we indicate that it is an exported field. Although it is not required if the package is imported.
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
You have to first import the package whose identifiers you want to refer to:
import "path/to/fsm"
Once you do this, the package name fsm
becomes a new identifier in your file block, and you can refer to its exported identifiers (identifiers that start with an uppercase letter) by using a qualified identifier, which is packagename.IdentifierName
like this:
Transitions := make(map[fsm.EKey]string)
See related question: Getting a use of package without selector error
try this
package main
import (
"encoding/json"
"fmt"
"github.com/looplab/fsm"
)
func main(){
Transitions := make(map[fsm.EKey]string)
}
You need to refer to your struct using fsm.EKey
If you want to import it to your local name space, you need a dot before the import path.
import (
// ....
. "github.com/looplab/fsm"
)
Now you can refer to your struct directly as EKey