<p>Let's consider your typical web application. In an MVC application, you may eventually want to introduce a "Service" layer that abstracts complex business logic such as user registration. So in your controller, you'd pass an instance of a <code>services.User</code> struct and simply call the <code>Register()</code> method on it. </p>
<p>Now, if <code>services.User</code> was simply a struct, we could have a relatively simple source code structure, like so:</p>
<pre><code>- [other directories here]/
- services/
- user.go
- [other service structs here]
- main.go
</code></pre>
<p>And the <code>services/user.go</code> would look like so : </p>
<pre><code>package services
type User struct { ... }
func NewUserService(){ ... }
func (u User) Register() { ... }
</code></pre>
<p>Which is all reasonably easy to read, so far. Let's say we take it one step further. In the spirit of making our web app easily testable, we'll turn all our Service structs into Service interfaces. That way, we can easily mock them for unit tests. For that purpose, we'll create a "AppUser" struct (for use in the actual application) and a "MapUser" struct (for mocking purposes). Placing the interfaces and the implementations in the same <code>services</code> directory makes sense - they're all still <code>service</code> code, after all.</p>
<p>Our <code>services</code> folder now looks like this:</p>
<pre><code>- services/
- app_user.go // the AppUser struct
- [other services here]
- map_user.go // the MapUser struct
- [other services here]
- user.go // the User interface
- [other service structs here]
</code></pre>
<p>As you can tell, this makes the <code>services</code> package and directory a lot more difficult to handle - you can easily imagine how chaotic it would look with a dozen different interfaces, each of which at least have at least 1 implementation. If I change the <code>User</code> interface in <code>user.go</code>, I'd have to dart all across the directory listing to find all it's implementations to change, which is not at all ideal.</p>
<p>Additionally, it becomes pretty crazy when you type <code>services.New(...)</code> and you're greeted with perhaps 50 or so autocomplete suggestions ; the <code>services</code> package has become nothing but a shambling monster.</p>
<p>One of the simplest ideas I had to solve this is to go against convention and embrace repetition: </p>
<pre><code>- services/
- userService/
- app.go // the AppUser struct
- map.go // the MapUser struct
- interface.go // the User interface
- [other services here]
</code></pre>
<p>This keeps all the UserService related code in a logical, self contained package. But having to constantly refer to <code>userService.UserService</code> is pretty darn ugly.</p>
<p>I've looked at all kinds of web application templates, and none of them (beyond the ones that are incredibly barebones) have an elegant solution to this structural. Most (if not all) of them simply omit interfaces completely to solve it, which is unacceptable. </p>
<p>Any tips or hints?</p>
<hr/>**评论:**<br/><br/>Morgahl: <pre><p>What you have described here is generally called <code>stutter</code> and does definitely look ugly. The <code>go vet</code> tool will even warn you about it with the one exception of your example above. </p></pre>Aetheus: <pre><p>Yeah, I'm aware of that. It is pretty ugly, I agree. But the alternative is to have a chaotic <code>services</code> directory in this case. Placing interface and implementation code in the same directory makes sense in most other programming languages (since they'd probably live in their own self-contained subdirectory/subpackage), but trying to do so here while following Go's conventions causes directories to inflate to ridiculous sizes and become unreadable. </p>
<p>What, then, is the "Go way" of avoiding such bloat while still following conventions? </p></pre>Morgahl: <pre><p>Your current <code>userService.UserService</code> is actually idiomatic, though I would see <a href="/u/Slythe2o0" rel="nofollow">/u/Slythe2o0</a> 's reply for a more concise way. The issues with stutter come about when you do things like <code>userService.NewUserService</code> or <code>userService.UserServiceRegister</code>. </p></pre>Sythe2o0: <pre><p>So remove "service" from the subdirectory types and packages.</p>
<pre><code>- services/
- user/
</code></pre>
<p>Then it's <code>user.User</code>, which is generally an accepted form of stutter, and is also short.</p></pre>Aetheus: <pre><p>Doesn't that lose the "context" of the package, then? Glancing through the code quickly, a casual reader wouldn't know if the <code>user</code> package was referring to a <code>service</code>, or a <code>model</code>, or a <code>template</code> or a <code>controller</code>. </p>
<p>The naming conventions work reasonably well for libraries ( an imaginary "http.Router()" is fairly self explanatory and "pretty" code ), but I haven't actually figured out how to make code look good in web application code.</p></pre>drvd: <pre><p>Your question implies that a package like <code>user</code> should refer to either a service or a model or ... and never to all of them. But this is common in Go: What's related is grouped in a package.</p></pre>beekay24: <pre><p>here's what i do:</p>
<pre><code>/api
/user.go
/store
/user.go
/schema
/user.go
</code></pre>
<p>/schema defines all the relevant structs (with the appropriate tags) that my service will use.
- this includes the data that is exchanged between my service and db as well as the data from service to http response. Usually they are the same, but on some occasions you might want to do in-memory aggregations. As an example...</p>
<pre><code>type User struct {
Name string `json:"name" bson:"name"`
}
type AllUsersResponse struct {
Count int `json:"count"`
Users []User `json:"users"`
}
</code></pre>
<p>/store defines the Interface for all the interactions that my service supports</p>
<pre><code>type Store interface {
GetAllUsers() ([]*schema.User, error)
GetUser(name string) (*schema.User, error)
}
type MongoStore struct {
session *mgo.Session
}
func (m *MongoStore) GetUsers() ([]*schema.User, error) {
// m.session.C("users").Find({})
}
func (m *MongoStore) GetUser(name string) (*AllUsersResponse, error) {
// m.session.C("users").Find({"name": name})
}
</code></pre>
<p>/api defines the routes that my service will support (i.e. using echo)</p>
<pre><code>func SetupRoutes(e *echo.Echo) {
e.GET("/api/users", func(c echo.Context) error {
// users, err := store.GetUsers()
})
e.GET("/api/users/:name", func(c echo.Context) error {
// users, err := store.GetUser(e.Param("name"))
})
}
</code></pre>
<p>Of course I'm severely oversimplifying the code examples but what I'm hoping you'll pick up from this is a clear division of layers in data transfer when you organize your packages this way...</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传