Using go module 101, undefined types

100 views
Skip to first unread message

Tong Sun

unread,
Sep 11, 2023, 5:23:24 PM9/11/23
to golang-nuts
This is really a go module 101 question, as I'm trying to understand and solve the undefined types problem that I'm having.

I have:

$ head -1 go.mod
module backend

And in my internal/petstore/main.go file, I have:

package main

import api "backend/internal/petstore/interfaces/ports"

func main() {
petStore := api.NewPetStore()
}

And in my internal/petstore/interfaces/ports/type.go file, I have:

package api
type PetStore struct {...}

I think it should compile, but I'm getting:

$ go run internal/petstore/main.go
# command-line-arguments
internal/petstore/main.go:..:38: undefined: api.PetStore

and I have no idea how to fix it.

I also tried to build the whole project with `go build .`, but got

no Go files in <my project root folder>

Please help. thx.

Axel Wagner

unread,
Sep 11, 2023, 6:38:54 PM9/11/23
to Tong Sun, golang-nuts
The code you posted does not actually contain the string `api.PetStore`, so it seems to be incomplete. From extrapolation, I agree that it should work, so the issue must be in that incomplete part.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ee028af4-3de2-468c-9ef2-48d8a2ac24cbn%40googlegroups.com.

Brian Candler

unread,
Sep 12, 2023, 3:23:25 AM9/12/23
to golang-nuts
I also tried to build the whole project with `go build .`

To do that you would need:

mkdir -p bin
go build -o bin ./...

The following compiles fine for me. Note that in your original post the error talks about api.PetStore, but the code you showed was a call to api.NewPetStore. Incidentally, you might also want to read https://google.github.io/styleguide/go/best-practices#naming

==> ./go.mod <==
module backend

==> ./internal/petstore/main.go <==

package main

import api "backend/internal/petstore/interfaces/ports"

func main() {
_ = api.PetStore{}
}

==> ./internal/petstore/interfaces/ports/type.go <==
Reply all
Reply to author
Forward
0 new messages