Skip to content

Instantly share code, notes, and snippets.

@pszalawinski
Created January 17, 2021 14:06
Show Gist options
  • Save pszalawinski/49ee4e48ba35ede01a2c7be27ac4f753 to your computer and use it in GitHub Desktop.
Save pszalawinski/49ee4e48ba35ede01a2c7be27ac4f753 to your computer and use it in GitHub Desktop.
interfaces in go #go
DEFINING
in interfaces we are definig behaviors
func main() {
var consoleWriter ConsoleWriter = ConsoleWriter{}
consoleWriter.Write("siemanko!")
}
type Writer interface {
Write(string) (int, error)
}
type ConsoleWriter struct{}
func (cs ConsoleWriter) Write(input string) (int, error) {
message, err := fmt.Println(input)
return message, err
}
we can compose interfaces:
type Writer interface{
Write([]byte) (int, error)
}
type Closer interface{
Close() error
}
typer WriterCloser interface{
Writer
Closer
}
BEST PRACTISES
-many, small interfaces not one big
io.Writer
io.Reader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment