Created
January 17, 2021 14:06
-
-
Save pszalawinski/49ee4e48ba35ede01a2c7be27ac4f753 to your computer and use it in GitHub Desktop.
interfaces in go #go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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