Home > Mobile >  Go can't find imported file
Go can't find imported file

Time:01-30

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?

CodePudding user response:

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.

  •  Tags:  
  • Related