With following project structure
D:\src\go\my-app
+ internal\
| + utils.go
+ main.go
+ go.mod
with following file contents:
internal\utils.go:
package internal
func GetText() string {
return "hello world"
}
main.go:
package main
import (
"fmt"
"example.com/my_app/internal"
)
func main() {
fmt.Println(GetText())
}
go.mod:
module example.com/my_app
go 1.17
I’m getting following compilation error when running from the directory:
D:\src\go\my-app>go build
# example.com/my_app
.\main.go:5:2: imported and not used: "example.com/my_app/internal"
.\main.go:9:14: undefined: GetText
Any idea, what could be wrong?
>Solution :
Yes, the problem is quite clear:
-
You import
example.com/my_app/internalbut you don’t use it. If you used it, there’d beinternal.<something>somewhere in your main. -
GetText()does not exist: there is noGetText()function in your main package.
Solution:
Replace GetText() by internal.GetText(). Now example.com/my_app/internal is used and GetText() is found in this package.