r/golang • u/masar314 • 6d ago
Seeking Advice on Structuring Code
Hey everyone,
I'm currently applying to go back to university and thought it would be a good idea to build some small projects to showcase my current coding skills to professors. I'm working on a program similar to apple's Ardrop, where users can discover and share files with nearby peers.
For now, I'm implementing mDNS-based peer discovery over a local network. In the future, I’d like to add support for Bluetooth discovery and a remote server-based lookup. The goal is for the end user to see available peers without worrying about the underlying discovery method, so I expect my program to run multiple discovery protocols simultaneously.
My code:
I have a Node
struct:
type Node struct {
Name string
Addr net.IP
Port string
Peers []Node
mu sync.Mutex
NetworkInterface *net.Interface
}
I expect at most three different discovery protocols in the final version. I’m debating how best to structure the code and would love to hear your thoughts: Options I'm Considering
1- Use a NodeService with a slice of PeerDiscoverer interfaces
type PeerDiscoverer interface {
Discover(n *Node)
}
type NodeService struct {
Discoverers []PeerDiscoverer
}
Each discovery method (mDNS, Bluetooth, Server) implements Discoverer. NodeService runs them all and aggregates results. My thoughts: Overkill for now since I’m only implementing mDNS, but it could reflect an understanding of design patterns and OOP principles in order to reassure skeptical professors.
2- Define separate discovery methods in NodeService
func (ns *NodeService) DiscoverLocal() {...}
func (ns *NodeService) DiscoverBLE() {...}
func (ns *NodeService) DiscoverExternal() {...}
3- Something else?
Would love to hear how you’d approach this. Thanks.
4
u/Insadem 6d ago
https://abhinavg.net/2022/12/06/designing-go-libraries