The problem didnt show up on my Problem tab, but there is panic when i am trying to run it

I make 3 go files with each of them is using the same package, which was package main. It will look like this...

mydir/
-client.go 
-room.go
-main.go

In my terminal, there is no problem that appears on it, but when I try to run/build main.go, it's returning a panic:

undefined: newRoom

here was my snippet code which has panic :

//main.go
r := newRoom()

//room.go
func newRoom() *room{
   return &room{}
}

When I try to delete the function, it's returning an error "Undeclared" in Visual Code. When adding it again, the problem disappears but when I try to run or build it, it returns an error if my newRoom function is undefined. Does anybody have a clue, what's wrong with it?

It's better if answered with some explanation why it doesn't appear on Visual Code Problems output.


Solution 1:

When you try to build main.go the compiler can see only the main.go file that why error was shown like "undefined new room" you should use go build . instead of go build main.go if you have multiple go file in first level of your directory.

Solution 2:

Here is the command to build a binary from a directory with a bunch of files:

go build ./mydir/...

This command build the package in the mydir folder and all packages recursing down. Run go help build to get more about build command.

There is no any issue with your code.