matryer / goblueprints

Source code for Go Programming Blueprints
1.49k stars 356 forks source link

chapter 1 chat undefined: newRoom #69

Open dprigh opened 4 years ago

dprigh commented 4 years ago

$ 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?

hongliu9527 commented 4 years ago

$ 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。

dprigh commented 4 years ago

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

thank you!

ZylleDK commented 4 years ago

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

ZylleDK commented 4 years ago

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"