Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Go can't find imported file

With following project structure

D:\src\go\my-app
 + internal\
 |  + utils.go
 + main.go
 + go.mod

with following file contents:

internal\utils.go:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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/internal but you don’t use it. If you used it, there’d be internal.<something> somewhere in your main.

  • GetText() does not exist: there is no GetText() 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading