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

  • 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)
}
主站蜘蛛池模板: 新丰县| 车险| 淅川县| 永仁县| 鄂伦春自治旗| 基隆市| 郎溪县| 三门县| 铜陵市| 台前县| 巴东县| 和龙市| 浏阳市| 东台市| 姜堰市| 教育| 高唐县| 团风县| 崇礼县| 鹤庆县| 临夏县| 隆化县| 甘泉县| 岚皋县| 寿阳县| 乌拉特中旗| 南丹县| 东乌珠穆沁旗| 金寨县| 陆河县| 通山县| 两当县| 镇雄县| 文安县| 湟源县| 灵川县| 马鞍山市| 砚山县| 密山市| 东宁县| 博罗县|