Created
September 5, 2017 11:10
-
-
Save samuell/a04aecc2b40f1e06dff68c7e27709f6a to your computer and use it in GitHub Desktop.
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 | |
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