Skip to content

Instantly share code, notes, and snippets.

@samuell
Created September 5, 2017 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuell/a04aecc2b40f1e06dff68c7e27709f6a to your computer and use it in GitHub Desktop.
Save samuell/a04aecc2b40f1e06dff68c7e27709f6a to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
// Create a new concrete IP of type `fileIP`, and populate with two tags
aFileIp := &fileIp{tags: map[string]string{"name": "Rolf", "likes": "Coffee"}}
// Call the function printTags(), which takes the interface `IP`
printTags(aFileIp)
}
// Function that takes the *interface* `IP`, and calls methods of the interface
func printTags(ip IP) {
fmt.Println("Tags:")
for tagName, tagValue := range ip.GetTags() {
fmt.Println("Tag name: ", tagName)
fmt.Println("Tag value:", tagValue, "\n")
}
}
// The IP interface
type IP interface {
GetTags() map[string]string
GetTag(tagName string) string
SetTag(tagName string, tagValue string)
}
// A concrete IP type, fulfilling the IP interface
type fileIp struct {
tags map[string]string
}
func (ip *fileIp) GetTags() map[string]string {
return ip.tags
}
func (ip *fileIp) GetTag(tagName string) string {
return ip.tags[tagName]
}
func (ip *fileIp) SetTag(tagName string, tagValue string) {
ip.tags[tagName] = tagValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment