Golang 開發使用了Liteide這個比較初級的IDE(剛開始的時候只找到這個IDE)。後來逐漸有了Golangd,還是一個月試用的那種,這裡有幾個小技巧,推薦一下。
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->interface
2. 使用模板3.1 forr 快速展開for rangeforr 然後tab,就會自動展開
for key, value := range collection { }
3.2 err 錯誤處理
err 然後tab,自動展開如下:
4. 填充Struct這個相對不是很實用,
5. 自動生成測試程式碼這個非常使用,單元測試,我們專注於測試本身就ok了.在檔案任意位置->Genreate->Test for File-> 自動生成該檔案對應的測試檔案
最新評論