Interface naming convention in Go language: start with a capital letter, use the "I" prefix to indicate the interface, and provide a descriptive name, such as IReader to indicate the reader interface.
The naming convention of interfaces in Go language
The naming of interfaces in Go language follows the following rules:
Reader
interface could be named IReader
. IDataReader
. Practical Case
Consider the following example:
// 定义一个表示读取器的接口 type IReader interface { Read() ([]byte, error) } // 定义一个实现 IReader 接口的结构体 type FileReader struct { file *os.File } // FileReader 实现 Read() 方法 func (f *FileReader) Read() ([]byte, error) { return ioutil.ReadAll(f.file) }
In this example, the IReader
interface is in uppercase letters beginning, and use the "I" prefix to indicate the interface. FileReader
implements this interface, which also follows the naming convention of the interface.
The above is the detailed content of What is the naming convention for interfaces in Go language?. For more information, please follow other related articles on the PHP Chinese website!