快速實現golang interface
golang中的interface提供了一種非常方便的方式來達到代碼重用的目的。
幾乎大部分的項目中都會用到interface,在日常工作中,為了實現某個interface,
我發現我也在一直不斷的查詢GoDocs,只有不斷去查,才能知道這個interface中
有哪些方法,這無疑是非常浪費時間的。
例如,為了讓我的結構體是可hash的,實現一個hash接口是非常常見的操作。以下是hash
的GoDocs:
type Hash interface {
// Write (via the embedded io.Writer interface) adds more data to the running hash.
// It never returns an error.
io.Writer
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
Sum(b []byte) []byte
// Reset resets the Hash to its initial state.
Reset()
// Size returns the number of bytes Sum will return.
Size() int
// BlockSize returns the hash's underlying block size.
// The Write method must be able to accept any amount
// of data, but it may operate more efficiently if all writes
// are a multiple of the block size.
BlockSize() int
}
為了實現 hash 接口,我們需要同時實現上述五個方法。
今天我來給大家介紹一種快速實現一個接口的方法。
Impl可以自動的實現一個接口,這非常有用。
如何使用呢? 執行以一下命令即可:
impl <struct-name> <intreface-name>
還是以hash這個結構體為例,具體看一下如何使用impl。
impl Foo hash.Hash
輸出:
func (Foo) Write(p []byte) (n int, err error) {
panic("not implemented") // TODO: Implement
}
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
func (Foo) Sum(b []byte) []byte {
panic("not implemented") // TODO: Implement
}
// Reset resets the Hash to its initial state.
func (Foo) Reset() {
panic("not implemented") // TODO: Implement
}
// Size returns the number of bytes Sum will return.
func (Foo) Size() int {
panic("not implemented") // TODO: Implement
}
// BlockSize returns the hash's underlying block size.
// The Write method must be able to accept any amount
// of data, but it may operate more efficiently if all writes
// are a multiple of the block size.
func (Foo) BlockSize() int {
panic("not implemented") // TODO: Implement
}
執行完上面的命令后,我們沒有必要再去不斷查詢hash的DoDocs去看如何實現一個接口了。
想像一下,如果后面我們需要為Foo結構體實現一個新的接口sortable,將會怎么樣呢?同樣的
我們也可以使用impl來實現sort的interface,如下:
impl Foo sort.interface
輸出:
// Len is the number of elements in the collection.
func (Foo) Len() int {
panic("not implemented") // TODO: Implement
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (Foo) Less(i int, j int) bool {
panic("not implemented") // TODO: Implement
}
// Swap swaps the elements with indexes i and j.
func (Foo) Swap(i int, j int) {
panic("not implemented") // TODO: Implement
}
impl不但可以實現標准庫中定義的接口,同樣的,它也能實現我們自定義的接口。例如:go-mysql
提供了一個管理mysql數據庫的強大的工具集。下面我們為其中的"Handler"接口做一個快速實現:
$ go get github.com/siddontang/go-mysql/server
$ impl Foo server.Handler
輸出:
//handle COM_INIT_DB command, you can check whether the dbName is valid, or other.
func (Foo) UseDB(dbName string) error {
panic("not implemented") // TODO: Implement
}
//handle COM_QUERY command, like SELECT, INSERT, UPDATE, etc...
//If Result has a Resultset (SELECT, SHOW, etc...), we will send this as the response, otherwise, we will send Result
func (Foo) HandleQuery(query string) (*server.Result, error) {
panic("not implemented") // TODO: Implement
}
//handle COM_FILED_LIST command
func (Foo) HandleFieldList(table string, fieldWildcard string) ([]*server.Field, error) {
panic("not implemented") // TODO: Implement
}
//handle COM_STMT_PREPARE, params is the param number for this statement, columns is the column number
//context will be used later for statement execute
func (Foo) HandleStmtPrepare(query string) (params int, columns int, context interface{}, err error) {
panic("not implemented") // TODO: Implement
}
//handle COM_STMT_EXECUTE, context is the previous one set in prepare
//query is the statement prepare query, and args is the params for this statement
func (Foo) HandleStmtExecute(context interface{}, query string, args []interface{}) (*server.Result, error) {
panic("not implemented") // TODO: Implement
}
//handle COM_STMT_CLOSE, context is the previous one set in prepare
//this handler has no response
func (Foo) HandleStmtClose(context interface{}) error {
panic("not implemented") // TODO: Implement
}
//handle any other command that is not currently handled by the library,
//default implementation for this method will return an ER_UNKNOWN_ERROR
func (Foo) HandleOtherCommand(cmd byte, data []byte) error {
panic("not implemented") // TODO: Implement
}
是不是很有用呢?
