Confused about code layout. I want to keep my frontend and backend code separate, but in the same git repo

agolangf · · 663 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Sorry if this has been addressed already. I&#39;ve done a fair amount of searching but i haven&#39;t found what a good answer to this.</p> <p>Suppose I have a go project like this</p> <pre><code>. ├── README.org ├── config │   └── config.go ├── finance │   └── finance.go ├── local │   ├── local.go │   └── twitter.go ├── main.go ├── traffic │   └── traffic.go └── weather └── weather.go </code></pre> <p>When i started out this was written as a command line app. main.go is a thin wrapper for the functions in the different packages. Now i&#39;d like to output to a redis database in addition to the current console output. I&#39;m sure i could add a switch and some additional logic to main.go but it seems like that would muddy things up eventually. I&#39;d like to have a separate executable that handles dumping data to redis.</p> <p>It seems like overkill to split out all of the frontend code into their own repos, but i cannot come up with a good scheme for supporting multiple &#34;frontends&#34; and backend code in one git repo. I&#39;d love to be able to do &#34;go build&#34; and &#34;go install&#34; and have to separate binaries built.</p> <p>Very grateful for your responses!</p> <hr/>**评论:**<br/><br/>hereatendill: <pre><p>One repo: <a href="https://github.com/golang/tools">https://github.com/golang/tools</a></p> <p>All the commands are in the cmd directory. So you can run &#34;go get golang.org/x/tools/cmd/...&#34; to install all the commands at once.</p></pre>uabassguy: <pre><p>I&#39;ve been setting up my applications in an MVC like fashion, where the models of business logic go in one folder, and the controllers and views in the others. Then I import them using import(&#34;model/modelname&#34;) and so on. Then you can use modelname.FunctionCall() to use them. Keeps my code nice and modular, and my main.go is only about 10 lines at most. The main package mostly just serves as a bootstrap and I can swap out packages as I see fit. </p></pre>SerialMiller: <pre><p>How do you do your dependency injection?</p></pre>uabassguy: <pre><p>Actually, so far, I havent really needed to. Each model handles its own job and called upon higher up in the chain. If they ever need to be aware of each other, I do so via imports at the package level. But for the most part they don&#39;t need to. You might find <a href="http://blog.parse.com/learn/engineering/dependency-injection-with-go/" rel="nofollow">this useful</a> </p></pre>roveboat: <pre><p>just have separate directories for the separate executables. </p> <p>main\main.go and redisclient\main.go (both in package main), which are then buildable with go build main/redisclient and the executables will be called main and redisclient.. or go build/install ./... to compile all your code, including the binaries. or put them in a separate frontend-directory and then you can do go build frontend/...</p></pre>cliffwarden: <pre><p>Thanks for the suggestions everyone. Much appreciated</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

663 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传