Last active
June 29, 2017 14:36
-
-
Save hartfordfive/1b7540f0d613e345e7114662305ce86d to your computer and use it in GitHub Desktop.
Simple Go Interfaces Example
This file contains hidden or 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 | |
// Run at https://play.golang.org/p/QDc3-94kqJ | |
import ( | |
"fmt" | |
"reflect" | |
) | |
type Event interface { | |
// The following two properties are the Event types required method signatures | |
SetMessage(msg string) | |
GetMessage() string | |
SetCode(c uint8) | |
GetCode() uint8 | |
Print() | |
} | |
type NoticeEvent struct { | |
message string | |
code uint8 | |
} | |
func (evt *NoticeEvent) SetMessage(msg string) { | |
evt.message = msg | |
} | |
func (evt *NoticeEvent) SetCode(c uint8) { | |
evt.code = c | |
} | |
func (evt *NoticeEvent) GetMessage() string { | |
return evt.message | |
} | |
func (evt *NoticeEvent) GetCode() uint8 { | |
return evt.code | |
} | |
func (evt *NoticeEvent) Print() { | |
fmt.Printf("[NOTICE] %s (%d)\n", evt.message, evt.code) | |
} | |
type ErrorEvent struct { | |
message string | |
code uint8 | |
} | |
func (evt *ErrorEvent) SetMessage(msg string) { | |
evt.message = msg | |
} | |
func (evt *ErrorEvent) SetCode(c uint8) { | |
evt.code = c | |
} | |
func (evt *ErrorEvent) GetMessage() string { | |
return evt.message | |
} | |
func (evt *ErrorEvent) GetCode() uint8 { | |
return evt.code | |
} | |
func (evt *ErrorEvent) Print() { | |
fmt.Printf("[ERROR] %s (%d)\n", evt.message, evt.code) | |
} | |
type CustomEvent struct { | |
message string | |
code uint8 | |
level string | |
} | |
func (evt *CustomEvent) SetMessage(msg string) { | |
evt.message = msg | |
} | |
func (evt *CustomEvent) SetCode(c uint8) { | |
evt.code = c | |
} | |
func (evt *CustomEvent) GetMessage() string { | |
return evt.message | |
} | |
func (evt *CustomEvent) GetCode() uint8 { | |
return evt.code | |
} | |
func (evt *CustomEvent) Print() { | |
fmt.Printf("[CUSTOM::%s] %s (%d)\n", evt.level, evt.message, evt.code) | |
} | |
func main() { | |
var evtNotice Event | |
evtNotice = &NoticeEvent{} | |
evtNotice.SetMessage("This is a sample notice") | |
evtNotice.SetCode(2) | |
if evt, ok := evtNotice.(*NoticeEvent); ok { | |
evt.Print() | |
fmt.Println(" -> Type:", reflect.TypeOf(evt)) | |
} | |
var evtError Event | |
evtError = &ErrorEvent{} | |
evtError.SetMessage("This is a sample error") | |
evtError.SetCode(4) | |
if evt, ok := evtError.(*ErrorEvent); ok { | |
evt.Print() | |
fmt.Println(" -> Type:", reflect.TypeOf(evt)) | |
} | |
var evtCustom Event | |
evtCustom = &CustomEvent{"This is a sample custom message which also has a level", 10, "debug"} | |
if evt, ok := evtCustom.(*CustomEvent); ok { | |
evt.Print() | |
fmt.Println(" -> Type:", reflect.TypeOf(evt)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment