Created
August 7, 2020 11:13
-
-
Save marceljay/164410eaea87b084af69bb5b6dbb5f5a to your computer and use it in GitHub Desktop.
GoLang: Interface As Parameter
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
package main | |
import ( | |
"fmt" | |
) | |
type Processor interface { | |
Process() | |
} | |
type Registry struct { | |
name string | |
} | |
// Type Registry implements Processor interface | |
func (r Registry) Process() { | |
fmt.Println("I print so I am") | |
} | |
// Another function takes the interface as an argument | |
func anotherFunction(p Processor) { | |
fmt.Println("I pretend to do extra stuff") | |
p.Process() | |
} | |
func main() { | |
var a Registry | |
a.name = "Alice" | |
anotherFunction(a) | |
describe(a) | |
} | |
func describe(i Processor) { | |
fmt.Printf("(%v, %T)\n", i, i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment