Note: I've seen other questions related to this topic but none that address the issue at hand.
I'm struggling to organize my Go code. I'm writing a daemon and I would like to split the code logically between files.
Say I have 3 files, main.go
:
package main
func main() {
GlobalFunc()
commonFunc()
var svr server.Server
server.OtherFunc()
}
server.go
:
package main
type Server struct {
name string
ip string
}
func GlobalFunc() {
}
func (s *Server) OtherFunc() {
commonFunc()
}
and common.go
:
package main
func commonFunc() {
}
I would like to be able to call GlobalFunc()
as server.GlobalFunc()
to show it's part of the "server module". That doesn't seem possible with the above example.
I could make a subdir server
, put server.go
in there, and change it to package server
. The problem here is that I can't seem to shared common.go
between the main and the server
package. I understand why, it doesn't seem like a good idea either.
So I am missing something simple here and making this more complicated?
Thanks!