matryer/goblueprints

chapter 1 chat undefined: newRoom

Opened this issue · 4 comments

$ go run main.go
$ go build main.go
command-line-arguments
./main.go: undefined: newRoom

$ go run room.go
$ go build room.go
command-line-arguments
./room.go: undefined: client

$ go run client.go
$ go build client.go
command-line-arguments
./client.go: undefined: room

$ go build -o chat
$ ./chat
chat can't run.

why? 'chat' file is not extension.
because add extension 'exe'
$ go build -o chat.exe
./chat.exe
Good run.

please saying.
What's wrong?

$ go run main.go
$ go build main.go
command-line-arguments
./main.go: undefined: newRoom

$ go run room.go
$ go build room.go
command-line-arguments
./room.go: undefined: client

$ go run client.go
$ go build client.go
command-line-arguments
./client.go: undefined: room

$ go build -o chat
$ ./chat
chat can't run.

why? 'chat' file is not extension.
because add extension 'exe'
$ go build -o chat.exe
./chat.exe
Good run.

please saying.
What's wrong?

you can build the chat.exe like this go build -o chat.exe main.go client.go room.go。

you can build the chat.exe like this go build -o chat.exe main.go client.go room.go。

thank you!

I had this problem too ...

You need to make sure your .go files are located inside a dirrectory, that is located inside the /src folder off your $GOPATH.

When calling go build, the golang only searches the src folder of your $GOPATH for files to include ... so that will fix your issue!

ex:
$GOPATH/src/chat/main.go

Also .. you only need to call "go build" in the folder with all your files ...
this will create and executable the the same name as the folder your working in (chat, if you did like the book)

then you can call "go run chat"