Your mymodule/mypackage.go file should start with package mypackage instead of package main. Generally, package main is for all of the files that live in the same folder as your main.go. There are exceptions, but you only use them when you know that you need to use them.
mymodule/mypackage.go also does not need a main function. That function is only used in your main package (usually in main.go) as the single entry point to your program.
When you run your package, use go run ..
go run main.go only compiles main.go, so the go command complains that it cannot find mymodule/mypackage. When you run go run ., the entire directory is inspected (including go.mod), so the compiler knows that mymodule/mypackage is inside of the mypackage directory.
Thank you so much for welcome and so good explanation.
Unfortunately I still have the problem. I fixed issues what you pointed out.
Now it looks like that:
❯ cat mymodule/mypackage.go
package mypackage
import "fmt"
func PrintHello() {
fmt.Println("Hello, Modules! This is mypackage speaking!")
}
❯ ls
go.mod main.go mymodule
❯ go run .
main.go:6:2: package mymodule/mypackage is not in GOROOT (/opt/homebrew/Cellar/go/1.19.5/libexec/src/mymodule/mypackage)
Your own modules need to have a “domain” that hosts them.
Usually it’s GitHub or GitLab. Though for your local experiments you can even use example.com, eg. example.com/mymodule.
Also make sure that your import actually states where to find the package, but it will be available under the declared package name in the importing scope!
So if you use the module name from my example, you would import as example.com/mymodule/mymodule and then use mypackage.PrintHello()!