I have a basic go project structure as follows:
src/github.com/jeeves/my-project/
|- main.go
|- models/
|- user.go
Contents of main.go
are -
package main
var log = initializeLogger()
func main() {
// do stuff
}
func initializeLogger() *logging.Logger {
// build and return a logger
}
Contents of models/user.go
are
package main
type User struct {
Firstname string `json:"first_name"`
Lastname string `json:"last_name"`
}
// other functions
I was curious about the best way to set up the package structure -
Should my
models/user.go
be part ofpackage models
instead ofpackage main
? What's the difference and how do I know when to use either?If I do use
package models
, how canmodels/user.go
access the globallog
object defined in themain.go
? (As you can see, I'm trying to define a logger once and have it accessible throughout my app.)
Thanks!