I have a script that pulls from a different data source depending on the user input with a general interface and a type for each data source. Each data source then has a method to get the meta data for that particular source. I'm struggling a bit understanding the idomatic Go implementation to switch types depending on input.
This example does not compile, but it was the version that best illustrates what I want to do:
type Post interface {
GetMetadata() bool
}
type YouTubeVideo struct {
ID string
Title string
ChannelID string
ChannelTitle string
PublishedAt string
}
func (ig *YouTubeVideo) GetMetadata() bool {
// ...
}
type InstagramPic struct {
ID string
ShortCode string
Type string
Title string
PublishedAt string
}
func (ig *InstagramPic) GetMetadata() bool {
// ...
}
func main() {
var thePost Post
switch domain {
case "youtube":
thePost = new(YouTubeVideo)
thePost.ID = pid
case "instagram":
thePost = new(InstagramPic)
thePost.ShortCode = pid
}
thePost.GetMetadata()
fmt.Println(thePost.title)
}