Go (Golang) Standard Library Interfaces (Selected)
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
.
(builtin)
doc] [src1] [src2]
error [type error interface {
Error() string
}
runtime
package doc] [src1] [src2]
Error [type Error interface {
error
RuntimeError()
}
math/rand
package doc] [src1] [src2]
Source [type Source interface {
Int63() int64
Seed(seed int64)
}
doc] [src1] [src2]
Source64 [type Source64 interface {
Source
Uint64() uint64
}
sort
package doc] [src1] [src2]
Interface [type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
container/heap
package doc] [src1] [src2]
Interface [type Interface interface {
sort.Interface
Push(x interface{})
Pop() interface{}
}
io
package doc] [src1] [src2]
Reader [type Reader interface {
Read(p []byte) (n int, err error)
}
doc] [src1] [src2]
Writer [type Writer interface {
Write(p []byte) (n int, err error)
}
doc] [src1] [src2]
Closer [type Closer interface {
Close() error
}
doc] [src1] [src2]
Seeker [type Seeker interface {
Seek(offset int64, whence int) (int64, error)
}
doc] [src1] [src2]
ReadWriter [type ReadWriter interface {
Reader
Writer
}
doc] [src1] [src2]
ReadCloser [type ReadCloser interface {
Reader
Closer
}
doc] [src1] [src2]
WriteCloser [type WriteCloser interface {
Writer
Closer
}
doc] [src1] [src2]
ReadWriteCloser [type ReadWriteCloser interface {
Reader
Writer
Closer
}
doc] [src1] [src2]
ReadSeeker [type ReadSeeker interface {
Reader
Seeker
}
doc] [src1] [src2]
WriteSeeker [type WriteSeeker interface {
Writer
Seeker
}
doc] [src1] [src2]
ReadWriteSeeker [type ReadWriteSeeker interface {
Reader
Writer
Seeker
}
doc] [src1] [src2]
ReaderFrom [type ReaderFrom interface {
ReadFrom(r Reader) (n int64, err error)
}
doc] [src1] [src2]
WriterTo [type WriterTo interface {
WriteTo(w Writer) (n int64, err error)
}
doc] [src1] [src2]
ReaderAt [type ReaderAt interface {
ReadAt(p []byte, off int64) (n int, err error)
}
doc] [src1] [src2]
WriterAt [type WriterAt interface {
WriteAt(p []byte, off int64) (n int, err error)
}
doc] [src1] [src2]
ByteReader [type ByteReader interface {
ReadByte() (byte, error)
}
doc] [src1] [src2]
ByteScanner [type ByteScanner interface {
ByteReader
UnreadByte() error
}
doc] [src1] [src2]
ByteWriter [type ByteWriter interface {
WriteByte(c byte) error
}
doc] [src1] [src2]
RuneReader [type RuneReader interface {
ReadRune() (r rune, size int, err error)
}
doc] [src1] [src2]
RuneScanner [type RuneScanner interface {
RuneReader
UnreadRune() error
}
fmt
package doc] [src1] [src2]
State [type State interface {
Write(b []byte) (n int, err error)
Width() (wid int, ok bool)
Precision() (prec int, ok bool)
Flag(c int) bool
}
doc] [src1] [src2]
Formatter [type Formatter interface {
Format(f State, c rune)
}
doc] [src1] [src2]
Stringer [type Stringer interface {
String() string
}
doc] [src1] [src2]
GoStringer [type GoStringer interface {
GoString() string
}
doc] [src1] [src2]
ScanState [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)
}
doc] [src1] [src2]
Scanner [type Scanner interface {
Scan(state ScanState, verb rune) error
}
encoding
package doc] [src1] [src2]
BinaryMarshaler [type BinaryMarshaler interface {
MarshalBinary() (data []byte, err error)
}
doc] [src1] [src2]
BinaryUnmarshaler [type BinaryUnmarshaler interface {
UnmarshalBinary(data []byte) error
}
doc] [src1] [src2]
TextMarshaler [type TextMarshaler interface {
MarshalText() (text []byte, err error)
}
doc] [src1] [src2]
TextUnmarshaler [type TextUnmarshaler interface {
UnmarshalText(text []byte) error
}
net
package doc] [src1] [src2]
Addr [type Addr interface {
Network() string
String() string
}
doc] [src1] [src2]
Conn [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
}
doc] [src1] [src2]
PacketConn [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
}
doc] [src1] [src2]
Listener [type Listener interface {
Accept() (Conn, error)
Close() error
Addr() Addr
}
doc] [src1] [src2]
Error [type Error interface {
error
Timeout() bool
Temporary() bool
}
net/http
package doc] [src1] [src2]
RoundTripper [type RoundTripper interface {
RoundTrip(*Request) (*Response, error)
}
doc] [src1] [src2]
FileSystem [type FileSystem interface {
Open(name string) (File, error)
}
doc] [src1] [src2]
File [type File interface {
io.Closer
io.Reader
io.Seeker
Readdir(count int) ([]os.FileInfo, error)
Stat() (os.FileInfo, error)
}
doc] [src1] [src2]
Pusher [type Pusher interface {
Push(target string, opts *PushOptions) error
}
doc] [src1] [src2]
CookieJar [type CookieJar interface {
SetCookies(u *url.URL, cookies []*Cookie)
Cookies(u *url.URL) []*Cookie
}
doc] [src1] [src2]
Handler [type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
doc] [src1] [src2]
ResponseWriter [type ResponseWriter interface {
Header() Header
Write([]byte) (int, error)
WriteHeader(int)
}
doc] [src1] [src2]
Flusher [type Flusher interface {
Flush()
}
doc] [src1] [src2]
Hijacker [type Hijacker interface {
Hijack() (net.Conn, *bufio.ReadWriter, error)
}
doc] [src1] [src2]
CloseNotifier [type CloseNotifier interface {
CloseNotify() <-chan bool
}
image
package doc] [src1] [src2]
Image [type Image interface {
ColorModel() color.Model
Bounds() Rectangle
At(x, y int) color.Color
}
doc] [src1] [src2]
PalettedImage [type PalettedImage interface {
ColorIndexAt(x, y int) uint8
Image
}
image/color
package doc] [src1] [src2]
Color [type Color interface {
RGBA() (r, g, b, a uint32)
}
doc] [src1] [src2]
Model [type Model interface {
Convert(c Color) Color
}
image/draw
package doc] [src1] [src2]
Image [type Image interface {
image.Image
Set(x, y int, c color.Color)
}
doc] [src1] [src2]
Quantizer [type Quantizer interface {
Quantize(p color.Palette, m image.Image) color.Palette
}
doc] [src1] [src2]
Drawer [type Drawer interface {
Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point)
}
hash
package doc] [src1] [src2]
Hash [type Hash interface {
io.Writer
Sum(b []byte) []byte
Reset()
Size() int
BlockSize() int
}
doc] [src1] [src2]
Hash32 [type Hash32 interface {
Hash
Sum32() uint32
}
doc] [src1] [src2]
Hash64 [type Hash64 interface {
Hash
Sum64() uint64
}
crypto
package doc] [src1] [src2]
Signer [type Signer interface {
Public() PublicKey
Sign(rand io.Reader, digest []byte, opts SignerOpts) (signature []byte, err error)
}
doc] [src1] [src2]
SignerOpts [type SignerOpts interface {
HashFunc() Hash
}
doc] [src1] [src2]
Decrypter [type Decrypter interface {
Public() PublicKey
Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error)
}
reflect
package doc] [src1] [src2]
Type [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
}
os
package doc] [src1] [src2]
Signal [type Signal interface {
String() string
Signal()
}
doc] [src1] [src2]
FileInfo [type FileInfo interface {
Name() string
Size() int64
Mode() FileMode
ModTime() time.Time
IsDir() bool
Sys() interface{}
}
Source
iface.awk
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"
}
}
}
make.sh
#!/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'
This comment has been minimized.
Very useful . Thank you so much