Interface Type

Interface names conventions

By convention, one-method interfaces are named by the method name plus an -er suffix.

type Greetinger interface {
    Greeting() string
}

Define an interface type

It start with the keyword “type” follow by the interface name (ended with -er). Begin of a block of code symbol “{“, the function name (eg. Greeting() function and return a string ). One or more than one function can be added here then is often called a method set and ending of a block of code “}”.

// https://golang.org/pkg/os/#FileInfo

type FileInfo interface {
        Name() string       // base name of the file
        Size() int64        // length in bytes for regular files; system-dependent for others
        Mode() FileMode     // file mode bits
        ModTime() time.Time // modification time
        IsDir() bool        // abbreviation for Mode().IsDir()
        Sys() interface{}   // underlying data source (can return nil)
}

In the above OS package, has FileInfo interface contains the following method sets

  • Name() string
  • Size() int64
  • Mode() FileMode
  • ModTime() time.Time
  • IsDir() bool
  • Sys() interface{}

Implementing an Interface