How to include external file in Go?

2019-10-04 06:21发布

问题:

I'm using LiteIDE for Go. I have a Go file located here:

/Users/username/go/src/src/Helper/Helper.go

When I include the file using:

import "../Helper"

I get this error:

can't load package: /Users/username/go/src/src/projectA/main.go:4:8: local import "../Helper" in non-local package

Any ideas what I'm doing wrong?

回答1:

You import packages by import path. For package Helper, located in $GOPATH/src/Helper/, use:

import "Helper"

While they can work in some cases, relative paths aren't supported by the go toolchain, and are discouraged.



标签: go package