Created
October 18, 2015 10:58
-
-
Save hoenirvili/a8ecf04998a2075ea060 to your computer and use it in GitHub Desktop.
Example of using interfaces in 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
package main | |
import ( | |
"log" | |
) | |
type Notifier interface { | |
Notify() error | |
} | |
//our User struct | |
type User struct { | |
Name string | |
Email string | |
} | |
//basic Handler | |
func SendNotification(notify Notifier) error { | |
return notify.Notify() | |
} | |
// define our user method to notify us | |
func (u *User) Notify() error { | |
log.Printf("User: Sengind User, Mail to %s<%s>\n", | |
u.Name, | |
u.Email) | |
return nil | |
} | |
func main() { | |
user:= &User { | |
Name: "Alber Jones", | |
Email: "albJones@email.com", | |
} | |
SendNotification(user) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment