Tag Archives: cannot find package

Go build cannot find package Error [How to Solve]

go env key data are as follows
GOPATH="/home/zzy/goProject"
GOROOT="/usr/local/go"

The project directory is like this

goProject
├── bin
├── pkg
└── src
    └── go_learn
            └── day01
                ├── hello
                │   ├── hello
                │   └── hello.go
                └── str_type
                    ├── str.go
                    └── world.go
hello.go my code is like this
package main

import (
    "fmt"
    "go_learn/day01/str_type/world"    ## This sentence imports packages and keeps reporting errors
)
func main()  {
    fmt.Print("hello world")
    test()
}

The

world.go code is like this
package main

import "fmt"

func test()  {
    fmt.Print("LALALALA")

}
go build reports an error like this
$ go build
hello.go:5:2: cannot find package "go_learn/day01/str_type/world" in any of:
        /usr/local/go/src/go_learn/day01/str_type/world (from $GOROOT)
        /home/zzy/goProject/src/go_learn/day01/str_type/world (from $GOPATH)

finally solved! A very low problem. It is estimated that beginners will make

Just change the content of the world.go file

package  world  # It's a file, not a package, don't import main

import "fmt"

# The function name is capitalized so you can reference the function outside
func Test()  {
    fmt.Println("lll")
    
}