Skip to content

Instantly share code, notes, and snippets.

@marceljay
Created August 7, 2020 11:13
Show Gist options
  • Save marceljay/164410eaea87b084af69bb5b6dbb5f5a to your computer and use it in GitHub Desktop.
Save marceljay/164410eaea87b084af69bb5b6dbb5f5a to your computer and use it in GitHub Desktop.
GoLang: Interface As Parameter
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