五個goland進行go開發的小技巧
阿新 • • 發佈:2019-05-09
alc 開發 面向接口編程 eal top difficult sta med 自動展開
五個goland進行go開發的小技巧
本文譯自5 Tips To Speed Up Golang Development With IntelliJ Or Goland 確實很實用.
1. 實現interface
比如我想為下面的結構體實現共識interface
type MyConensus struct {
}
通過右鍵generate->implement methods->搜索engine
一鍵生成下面代碼:
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") }
提取接口
面向接口編程,有時候我們需要針對已經實現的struct提取接口.
方法:
struct->Refactor->Extract->interfac
2. 使用模板
3.1 forr 快速展開for range
forr 然後tab,就會自動展開
for key, value := range collection {
}
3.2 err 錯誤處理
err 然後tab,自動展開如下:
4. 填充Struct
這個相對不是很實用,
5. 自動生成測試代碼
這個非常使用,單元測試,我們專註於測試本身就ok了.
在文件任意位置->Genreate->Test for File-> 自動生成該文件對應的測試文件
五個goland進行go開發的小技巧