官术网_书友最值得收藏!

  • 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)
}
主站蜘蛛池模板: 定兴县| 呼伦贝尔市| 合水县| 铜山县| 宁武县| 获嘉县| 玉林市| 东丽区| 卓尼县| 信丰县| 格尔木市| 武安市| 凌海市| 七台河市| 南城县| 湛江市| 郑州市| 北辰区| 孟连| 科技| 潍坊市| 曲松县| 信宜市| 鲁山县| 邢台县| 股票| 左贡县| 游戏| 如东县| 马关县| 西乌珠穆沁旗| 三明市| 滕州市| 红原县| 建德市| 神农架林区| 白水县| 内乡县| 昆山市| 英吉沙县| 榆社县|