This is not an exhaustive list of all interfaces in Go's standard library.
I only list those I think are important.
Interfaces defined in frequently used packages (like io
, fmt
) are included.
Interfaces that have significant importance are also included.
All of the following information is based on go version go1.8.3 darwin/amd64
.
type error interface {
Error() string
}
type Error interface {
error
RuntimeError()
}
type Source interface {
Int63() int64
Seed(seed int64)
}
type Source64 interface {
Source
Uint64() uint64
}
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
type Interface interface {
sort.Interface
Push(x interface{})
Pop() interface{}
}
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type Closer interface {
Close() error
}
type Seeker interface {
Seek(offset int64, whence int) (int64, error)
}
type ReadWriter interface {
Reader
Writer
}
type ReadCloser interface {
Reader
Closer
}
type WriteCloser interface {
Writer
Closer
}
type ReadWriteCloser interface {
Reader
Writer
Closer
}
type ReadSeeker interface {
Reader
Seeker
}
type WriteSeeker interface {
Writer
Seeker
}
type ReadWriteSeeker interface {
Reader
Writer
Seeker
}
type ReaderFrom interface {
ReadFrom(r Reader) (n int64, err error)
}
type WriterTo interface {
WriteTo(w Writer) (n int64, err error)
}
type ReaderAt interface {
ReadAt(p []byte, off int64) (n int, err error)
}
type WriterAt interface {
WriteAt(p []byte, off int64) (n int, err error)
}
type ByteReader interface {
ReadByte() (byte, error)
}
type ByteScanner interface {
ByteReader
UnreadByte() error
}
type ByteWriter interface {
WriteByte(c byte) error
}
type RuneReader interface {
ReadRune() (r rune, size int, err error)
}
type RuneScanner interface {
RuneReader
UnreadRune() error
}
type State interface {
Write(b []byte) (n int, err error)
Width() (wid int, ok bool)
Precision() (prec int, ok bool)
Flag(c int) bool
}
type Formatter interface {
Format(f State, c rune)
}
type Stringer interface {
String() string
}
type GoStringer interface {
GoString() string
}
type ScanState interface {
ReadRune() (r rune, size int, err error)
UnreadRune() error
SkipSpace()
Token(skipSpace bool, f func(rune) bool) (token []byte, err error)
Width() (wid int, ok bool)
Read(buf []byte) (n int, err error)
}
type Scanner interface {
Scan(state ScanState, verb rune) error
}
type BinaryMarshaler interface {
MarshalBinary() (data []byte, err error)
}
type BinaryUnmarshaler interface {
UnmarshalBinary(data []byte) error
}
type TextMarshaler interface {
MarshalText() (text []byte, err error)
}
type TextUnmarshaler interface {
UnmarshalText(text []byte) error
}
type Addr interface {
Network() string
String() string
}
type Conn interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
Close() error
LocalAddr() Addr
RemoteAddr() Addr
SetDeadline(t time.Time) error
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
}
type PacketConn interface {
ReadFrom(b []byte) (n int, addr Addr, err error)
WriteTo(b []byte, addr Addr) (n int, err error)
Close() error
LocalAddr() Addr
SetDeadline(t time.Time) error
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
}
type Listener interface {
Accept() (Conn, error)
Close() error
Addr() Addr
}
type Error interface {
error
Timeout() bool
Temporary() bool
}
type RoundTripper interface {
RoundTrip(*Request) (*Response, error)
}
type FileSystem interface {
Open(name string) (File, error)
}
type File interface {
io.Closer
io.Reader
io.Seeker
Readdir(count int) ([]os.FileInfo, error)
Stat() (os.FileInfo, error)
}
type Pusher interface {
Push(target string, opts *PushOptions) error
}
type CookieJar interface {
SetCookies(u *url.URL, cookies []*Cookie)
Cookies(u *url.URL) []*Cookie
}
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
type ResponseWriter interface {
Header() Header
Write([]byte) (int, error)
WriteHeader(int)
}
type Flusher interface {
Flush()
}
type Hijacker interface {
Hijack() (net.Conn, *bufio.ReadWriter, error)
}
type CloseNotifier interface {
CloseNotify() <-chan bool
}
type Image interface {
ColorModel() color.Model
Bounds() Rectangle
At(x, y int) color.Color
}
type PalettedImage interface {
ColorIndexAt(x, y int) uint8
Image
}
type Color interface {
RGBA() (r, g, b, a uint32)
}
type Model interface {
Convert(c Color) Color
}
type Image interface {
image.Image
Set(x, y int, c color.Color)
}
type Quantizer interface {
Quantize(p color.Palette, m image.Image) color.Palette
}
type Drawer interface {
Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
}
type Hash interface {
io.Writer
Sum(b []byte) []byte
Reset()
Size() int
BlockSize() int
}
type Hash32 interface {
Hash
Sum32() uint32
}
type Hash64 interface {
Hash
Sum64() uint64
}
type Signer interface {
Public() PublicKey
Sign(rand io.Reader, digest []byte, opts SignerOpts) (signature []byte, err error)
}
type SignerOpts interface {
HashFunc() Hash
}
type Decrypter interface {
Public() PublicKey
Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error)
}
type Type interface {
Align() int
FieldAlign() int
Method(int) Method
MethodByName(string) (Method, bool)
NumMethod() int
Name() string
PkgPath() string
Size() uintptr
String() string
Kind() Kind
Implements(u Type) bool
AssignableTo(u Type) bool
ConvertibleTo(u Type) bool
Comparable() bool
Bits() int
ChanDir() ChanDir
IsVariadic() bool
Elem() Type
Field(i int) StructField
FieldByIndex(index []int) StructField
FieldByName(name string) (StructField, bool)
FieldByNameFunc(match func(string) bool) (StructField, bool)
In(i int) Type
Key() Type
Len() int
NumField() int
NumIn() int
NumOut() int
Out(i int) Type
common() *rtype
uncommon() *uncommonType
}
type Signal interface {
String() string
Signal()
}
type FileInfo interface {
Name() string
Size() int64
Mode() FileMode
ModTime() time.Time
IsDir() bool
Sys() interface{}
}
BEGIN {
if (package == "") {
print "error: package is not defined"
exit 1
}
if (branch == "") {
print "error: branch is not defined"
exit 1
}
state = 0
indent0 = 0
indent1 = 0
from_line = 0
to_line = 0
filename = ""
type = ""
code = ""
printf "\n"
printf "\n"
printf "\n"
if (package == "builtin") {
printf "### (builtin)\n"
} else {
printf "### package `%s`\n", package
}
}
# Start of type
/type ([A-Z][^ ]*|error) interface {/ {
if (state == 0) {
indent0 = index($0, "type ")
if (indent0 == 1) {
state = 1
s = substr($0, indent0 + 5) # length("type ") == 5
len = index(s, " ") - 1
type = substr(s, 0, len)
filename = FILENAME
sub(/.*\//, "", filename)
from_line = FNR
code = ""
} else {
# >>> Uncomment this block to write an alert instead of skipping the code <<<
# state = 1
#
# s = substr($0, indent0 + 5) # length("type ") == 5
# len = index(s, " ") - 1
# type = substr(s, 0, len)
#
# filename = FILENAME
# sub(/.*\//, "", filename)
#
# from_line = FNR
# code = ""
#
# printf "\n# type is not the first character: filename: %s line: %d\n\n", FILENAME, FNR
}
}
}
# Inside type
{
if (state == 1) {
line = $0
# Remove comments
sub(/[\t ]*\/\/.*/, "", line)
# Remove trailing whitespaces
sub(/[\t ]*$/, "", line)
# Only print non-blank lines
if (line != "") {
code = code line "\n"
}
}
}
# End of type
/}/ {
if (state == 1) {
indent1 = index($0, "}")
if (indent0 == indent1) {
state = 0
to_line = FNR
printf "\n"
printf "#### %s " \
"[[doc](https://golang.org/pkg/%s/#%s)] " \
"[[src1](https://golang.org/src/%s/%s#L%d)] " \
"[[src2](https://github.com/golang/go/blob/release-branch.%s/src/%s/%s#L%d-L%d)]\n",
type,
package, type,
package, filename, from_line,
branch, package, filename, from_line, to_line
printf "\n"
printf "```go\n"
printf "%s", code
printf "```\n"
}
}
}
#!/bin/sh
packages=(
'builtin'
'runtime'
'math/rand'
'sort'
'container/heap'
'io'
'fmt'
'encoding'
'net'
'net/http'
'image'
'image/color'
'image/draw'
'hash'
'crypto'
'reflect'
'os'
)
if [ -z "${GOROOT}" ]
then
eval $(go env | grep -e '^GOROOT=')
fi
if [ -z "${GOROOT}" ]
then
echo 'Cannot find GOROOT'
exit 1
fi
go_version=$(go version)
go_branch=${go_version#go version }
go_branch=${go_branch% *}
case ${go_branch} in
go[0-9].[0-9])
;;
go[0-9].[0-9].[0-9])
go_branch=${go_branch%.[0-9]}
;;
*)
printf 'Unexpected go version: %s\n' ${go_version}
exit 2
;;
esac
echo '# Go (Golang) Standard Library Interfaces (Selected)'
echo
echo "This is not an exhaustive list of all interfaces in Go's standard library."
echo 'I only list those I think are important.'
echo 'Interfaces defined in frequently used packages (like `io`, `fmt`) are included.'
echo 'Interfaces that have significant importance are also included.'
echo
printf 'All of the following information is based on `%s`.\n' "$(go version)"
for package in ${packages[@]}
do
find ${GOROOT}/src/${package} -maxdepth 1 \
-type f '(' \
-name '*_test.go' -prune -o \
-name '*.go' -exec \
awk -f iface.awk -v package="${package}" -v branch="${go_branch}" '{}' '+' \
')'
done
printf '\n'
printf '\n'
printf '\n'
printf '\n'
printf '\n'
printf '## Source\n'
printf '\n'
printf '### iface.awk\n'
printf '\n'
printf '```awk\n'
cat iface.awk
printf '```\n'
printf '\n'
printf '### make.sh\n'
printf '\n'
printf '```sh\n'
cat make.sh
printf '```\n'
Very useful . Thank you so much