'How should I organise structs, variables and interfaces in Go?

I have a codebase where one file contains quite a lot of Structs, Interfaces and Variables in the same file as functions and I'm not sure if I need to seperate this into separate files with appending filename. So for example accounts.go will be accounts_struct.go and accounts_interface.go with struct and interface respectively.

What would be a good approach for the file organisation when you have growing codebase for Structs, Variables and Interfaces?

go


Solution 1:[1]

Here is my mental model for designing a package.

a. A package should encompass one idea or concept. http is a concept, http client or http message is not.

b. A file in a package should encompass a set of related types, a good rule of thumb is if two files share the same set of imports, merge them. Using the previous example, http/client.go, http/server.go are a good level of granularity

c. Don't do one file per type, that's not idiomatic Go.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dave Cheney