五个goland进行go开发的小技巧
<h1 id="五个goland进行go开发的小技巧">五个goland进行go开发的小技巧</h1><p>本文译自5 Tips To Speed Up Golang Development With IntelliJ Or Goland 确实很实用.</p>
<h2 id="1-实现interface">1. 实现interface</h2>
<p>比如我想为下面的结构体实现共识interface</p>
<pre><code class="language-go">
type MyConensus struct {
}
</code></pre>
<p>通过右键generate->implement methods->搜索engine<br>
一键生成下面代码:</p>
<pre><code class="language-go">type MyConensus struct {
info string
}
func (m *MyConensus) Author(header *types.Header) (common.Address, error) {
panic("implement me")
}
func (m *MyConensus) VerifyHeader(chain ChainReader, header *types.Header, seal bool) error {
panic("implement me")
}
func (m *MyConensus) VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
panic("implement me")
}
func (m *MyConensus) VerifyUncles(chain ChainReader, block *types.Block) error {
panic("implement me")
}
func (m *MyConensus) VerifySeal(chain ChainReader, header *types.Header) error {
panic("implement me")
}
func (m *MyConensus) Prepare(chain ChainReader, header *types.Header) error {
panic("implement me")
}
func (m *MyConensus) Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
panic("implement me")
}
func (m *MyConensus) Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
panic("implement me")
}
func (m *MyConensus) SealHash(header *types.Header) common.Hash {
panic("implement me")
}
func (m *MyConensus) CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int {
panic("implement me")
}
func (m *MyConensus) APIs(chain ChainReader) []rpc.API {
panic("implement me")
}
func (m *MyConensus) Close() error {
panic("implement me")
}
</code></pre>
<p><img src="https://img2018.cnblogs.com/blog/124391/201905/124391-20190509085750087-456762519.gif" alt="" loading="lazy"></p>
<h2 id="提取接口">提取接口</h2>
<p>面向接口编程,有时候我们需要针对已经实现的struct提取接口.<br>
方法:<br>
struct->Refactor->Extract->interfac<br>
<img src="https://img2018.cnblogs.com/blog/124391/201905/124391-20190509085812217-674004844.gif" alt="" loading="lazy"></p>
<h2 id="2-使用模板">2. 使用模板</h2>
<h3 id="31-forr-快速展开for-range">3.1 forr 快速展开for range</h3>
<p>forr 然后tab,就会自动展开</p>
<pre><code class="language-go"> for key, value := range collection {
}
</code></pre>
<p><img src="https://img2018.cnblogs.com/blog/124391/201905/124391-20190509085821223-1978977870.gif" alt="" loading="lazy"></p>
<h3 id="32-err-错误处理">3.2 err 错误处理</h3>
<p>err 然后tab,自动展开如下:<br>
<img src="https://img2018.cnblogs.com/blog/124391/201905/124391-20190509085831846-521727195.gif" alt="" loading="lazy"></p>
<h3 id="4-填充struct">4. 填充Struct</h3>
<p>这个相对不是很实用,<br>
<img src="https://img2018.cnblogs.com/blog/124391/201905/124391-20190509085841121-997810444.gif" alt="" loading="lazy"></p>
<h3 id="5-自动生成测试代码">5. 自动生成测试代码</h3>
<p>这个非常使用,单元测试,我们专注于测试本身就ok了.<br>
在文件任意位置->Genreate->Test for File-> 自动生成该文件对应的测试文件<br>
<img src="https://img2018.cnblogs.com/blog/124391/201905/124391-20190509085851547-53435913.gif" alt="" loading="lazy"></p><br><br>
来源:https://www.cnblogs.com/baizx/p/10836468.html
頁:
[1]