'Access inner fields on a generic in a type-safe way [duplicate]

Is there a way to access inner fields on a generic in a type-safe way?

I would have expected the following to work:

package main

import "fmt"

type Video struct {
    url string
}

type Text struct {
    url string
}

type Message interface {
    Video | Text
}

func getURL[M Message](msg M) string {
    return msg.url
}

func main() {
    fmt.Println(getURL(Video{url: "youtube.com/ID"}))
}

Is this not possible yet with Go generics?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source