- Security with Go
- John Daniel Leon
- 434字
- 2021-06-30 19:06:48
Interface
Interfaces are a special type that define a collection of function signatures. You can think of an interface as saying, "a type must implement function X and function Y to satisfy this interface." If you create any type and implement the functions needed to satisfy the interface, your type can be used anywhere that the interface is expected. You don't have to specify that you are trying to satisfy an interface, the compiler will determine if it satisfies the requirements.
You can add as many other functions as you want to your custom type. The interface defines the functions that are required, but it does not mean that your type is limited to implementing only those functions.
The most commonly used interface is the error interface. The error interface only requires a single function to be implemented, a function named Error() that returns a string with the error message. Here is the interface definition:
type error interface {
Error() string
}
This makes it very easy for you to implement your own error interfaces. This example creates a customError type and then implements the Error() function needed to satisfy the interface. Then, a sample function is created, which returns the custom error:
package main
import "fmt"
// Define a custom type that will
// be used to satisfy the error interface
type customError struct {
Message string
}
// Satisfy the error interface
// by implementing the Error() function
// which returns a string
func (e *customError) Error() string {
return e.Message
}
// Sample function to demonstrate
// how to use the custom error
func testFunction() error {
if true != false { // Mimic an error condition
return &customError{"Something went wrong."}
}
return nil
}
func main() {
err := testFunction()
if err != nil {
fmt.Println(err)
}
}
Other frequently used interfaces are the Reader and Writer interfaces. Each one only requires one function to be implemented in order to satisfy the interface requirements. The big benefit here is that you can create your own custom types that reads and writes data in some arbitrary way. The implementation details are not important to the interface. The interface won't care whether you are reading and writing to a hard disk, a network connection, storage in memory, or /dev/null. As long as you implement the function signatures that are required, you can use your type anywhere the interface is used. Here is the definition of the Reader and Writer interfaces:
type Reader interface {
Read(p []byte) (n int, err error)
} type Writer interface {
Write(p []byte) (n int, err error)
}
- Hands-On Blockchain Development in 7 Days
- 世界是隨機(jī)的:大數(shù)據(jù)時(shí)代的概率統(tǒng)計(jì)學(xué)
- GMAT批判性推理:邏輯分類精講
- 好玩的數(shù)學(xué)符號(hào)
- 你學(xué)的數(shù)學(xué)可能是假的
- 別說(shuō)你不懂?dāng)?shù)學(xué)
- 魔方的思維世界
- 救命的數(shù)學(xué)
- Blockchain for Decision Makers
- 10堂極簡(jiǎn)概率課
- 數(shù)字、代數(shù)和圖象(全彩版)
- 數(shù)學(xué)建模
- 這才是好讀的數(shù)學(xué)史
- Digital Forensics with Kali Linux
- 牛津通識(shí)讀本:數(shù)學(xué)(中文版)