As the title here is my current structure for Go app that serves static http page and also sends data through websocket.
In the structure below I have 2 packages constants and main and am unable to use constants from main package.
Server-Client Game
assets
css
index.css
js
app.js
templates
index.html
constants
server.go
game.go
main
main.go
hub.go
player.go
Should I be using Go workspace? or can I get it working as is?
My project currently is not in the GoPath src which I was previously calling 'workspace'.
you should have a gopath which should contain at least src
and bin
. your project source code will go into the src folder and your published binaries will go into your bin folder.
for instance, if your go path points to c:/go-apps then your project may look like this . . .
c:/go-apps/src/Server-Client Game/ ...
if you issue go install
, your binaries will be in c:/go-apps/bin
. since go also supports git repositories, you can import them directly into your project like this (assuming it is public)
import "github.com/gorilla/mux"
a statement like this will import the popular mux package from github. you can expect to find this in your gopath to be
c:/go-apps/src/github.com/gorilla/mux
also you may have multiple gopaths. when you compile an application, it will be looking in your gopath(s) for the package. if the package is not available it will automatically download them.
alternatively to go install, you can use go build
to build the binaries in the current folder. to simply download all the dependencies without compiling use go get
.
as for the folder structure, yes that should be fine, but i usually put my main in the root directory since package main
and func() main
is the entry point and only one package is allowed per directory in go.
You have to import
your constants
package in your main files. You import your own packages the same as any other non-standard package, by its path in your $GOPATH/src
directory. So if your project sits in the folder $GOPATH/src/myproject
, you would import your constants package like so:
import "myproject/constants"
And given an exported (uppercase) constant e.g. SomeConstant
, you would refer to it in your main package as constants.SomeConstant
.