Confused about packages

I’m learning Golang from Head first Go. This is my directory structure for practice code.

package example

type Date struct {
    day int
    month int
    year int
package example

type Event struct {
    title string

I’m facing Date: undefined in event.go even though they’re part of the same package. How to deal with this?

The headfirst book directly adds the example package to C:/go/src, the go SDK.
I don’t really want to make changes there.

Did you run:

go init <module_name>

Cos if you did, it should work.

Just create a module and avoid meddling with the go sdk at all, except you know exactly what you’re doing.

Thanks a ton for your reply!
I did this:

go mod init headfirst       //removed src directory from headfirst
go.mod                           //Added go.mod file
module headfirst
go 1.20

if I do this go run main.go it works only if main.go is inside headfirst module.
If I create main.go outside of module, then import doesn’t work.
How to handle this? I tried adding /headfirst to GOPATH, but came across this error:
$GOPATH/go.mod exists but should not.

Why would you want to have your main.go outside the module(headfirst)… ur workspace only recognizes the contents of that module, everything else that would come in would be a absolute path

If you’re interested in “how can two local modules talk to each other”. There’s something called -replace amidst the go command. Let me go look for it

Do through this example tutorial to see how modules can talk to each other

So if you want ur main.go outside headfirst but present in another module…and still talk to package in headfirst that example should be fine. good luck

What are you doing when you get that error?


Thanks for the reply. The issue is solved, it was a vscode intellisense issue.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.